0

I have 3 columns A, B, C and I want to make a column D with values in A, B, C but it should include ">=", "<=" signs as well. The script I am working on does help me loop around columns and copy its data to a new column. Can anyone help me figure out how I can add those special characters at the beginning of the numbers in the cells? Thanks for any help.

Sub Try()

With ActiveWorkbook.Sheets("Sheet1")
    For rw = 1 To .Rows.Count
        If (.Rows(rw).Columns("A:A").Value <> "") Then
            .Rows(rw).Columns("A:A").Copy .Range("D" & rw)
        End If
    Next rw
    .Columns("A:A").Delete

End With

End Sub

enter image description here

4
  • So the >= and <= are not in the data currently? You want to go through the pasted data, and add >= or <= to that data? How do you know what cell gets what sign? I would just loop through each cell in the copied range, adding the symbols, no? Commented Apr 13, 2018 at 14:18
  • 1
    @BruceWayne: from what I can gather from the example sheet, if text is in A, we add >=, if its column B its <= and if its column C, no sign is added. @PowerToYou, you can achieve this with formulas, is there a reason why you want to achieve this with VBA? Commented Apr 13, 2018 at 14:20
  • So, the symboles would depend on what columns the data would come from. For example, Column A could have ">=" and B could have "<=". The issue is I have about 12 columns and I don't know what would be the most efficient way to do this. @BruceWayne Commented Apr 13, 2018 at 14:21
  • Yes, I have already developed a macro which does other tasks. I only want this as an addition to the script I have already developed.@BruceWayne Commented Apr 13, 2018 at 14:22

4 Answers 4

3

With data in cols A through C, in D1 enter:

=IF(A1<>"",">="&A1,IF(B1<>"","<="&B1,C1))

and copy down:

enter image description here

EDIT#1:

To do this with VBA:

Sub PopulateFormulas()
    Dim N As Long, s As String

    s = "=IF(A1<>"""","">=""&A1,IF(B1<>"""",""<=""&B1,C1))"
    N = Range("A1").CurrentRegion.Rows.Count
    Range("D1:D" & N).Formula = s
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

My thought exactly. Don't need VBA for this.. easily achieved with formula
Plus one for simplicity :). If OP can use formulas then he should use this.
1

Probably not the most elegant solution (and you probably don't even need VBA for this, a formula would most likely suffice), but this does the trick:

Sub Test()

arr = Array(">=", "<=", "")

With ActiveWorkbook.Sheets("Sheet1")
    For cl = 1 To 3
        For rw = 2 To .Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
            If .Cells(rw, cl).Value <> "" Then
                .Cells(rw, 4).Value = arr(cl - 1) & .Cells(rw, cl).Value
            End If
        Next rw
    Next cl
End With

'If you still need to delete those columns at the end-
'ActiveWorkbook.Sheets("Sheet1").Columns("A:C").Delete xlShiftLeft
End Sub

pic1

Comments

0

This worked for me:

Sub macro_test()


k = 1

For k = 1 To 3
t = 2
lr = ActiveSheet.Cells(100000, k).End(xlUp).Row

Do Until t > lr

If Cells(t, k).Value = “” Then GoTo  continue

If k = 1 Then Cells(t, 4).Value = ">=" & Cells(t, k).Value
If k = 2 Then Cells(t, 4).Value = "<=" & Cells(t, k).Value
If k = 3 Then Cells(t, 4).Value = "" & Cells(t, k).Value
continue:
t = t + 1

Loop

Next

End Sub

6 Comments

Thank you so much for the help guys. One last question: What if I encounter an empty cell/cells? For example, say Column C is empty sometimes. How can I still make it work for Columns A and B?
you can add a "If lr=1 Then Resume Next" (the code is calculating last row, so if it's =1 that means, it's empty - only has the title on it)
Thanks a lot for the help @IRENE G this script only works for printing out the data from column A and still prints out ">=" for all other cells that are blank in column A, but are present in B or C
You may have blank cells in column A... I changed a little bit the code to skip these... hope it helps!
I still cannot print values from cells in the 2nd and 3rd columns. It leaves them blank. Only values from column A get printed out. @IRENE G
|
0

try this

Sub main()
    Dim iCol As Long, cell As Range, signs As Variant

    signs = Array(">=", "<=", "")
    For iCol = 1 To 3
        For Each cell In Columns(iCol).SpecialCells(xlCellTypeConstants, xlNumbers)
            cell.Value = signs(iCol - 1) & cell.Value                
        Next
    Next
End Sub

if your columns A, B and C not empty cells content is not a numeric one only, then you could use:

For Each cell In Columns(iCol).SpecialCells(xlCellTypeConstants)

while if it's some formula, then you could use:

For Each cell In Columns(iCol).SpecialCells(xlCellTypeFormulas)

4 Comments

Thanks a lot for helping, this throws an error on the For loop for cells part (i.e. the second for loop)
do you have "numbers" in your cells?
Yes, I have all numbers in my columns. They are either numbers or empty @DisplayName
Then my code should work as it is. But you may try changing SpecialCells parameter as per those other two alternatives I showed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.