Fill a column from a concatenate range - vba

What I have this far is below.
Range("F4").Value = Range("D4").Value & " x " & Range("E4")
This is creating a concatenate in Column F for me. This is working fine for the individual cell but I would like this to continue concatenating down until it hits an empty cell.
I have tried looping without success.
below is an image of the sheet

Set Concat= Rows(4)
concatstring =""
For i = 1 To Concat.Cells.Count
If Concat.Cells(i).Text <> "" Then
Concatstring = concatstring & " x " & Concat.Cells(i).Text
Else
Exit for
End If
Next
Concat.cells(I).text = concatstring
This should do it. Let me know if you still have problem.

Try this...
i=4
Do while(Range("D" & i).Value<> "")
Range("F" & i).Value = Range("D" & i).Value & " x " & Range("E" & i)
Loop

Related

operate cells with other sheet with sheet().cells() format

I need iterate sheets that plus values in their cells in other sheet, the names of the "sheets" are save in sheet().index= man_prov and i iterate that. The code works but appears a window that requets "refresh values to the sheets" and the objetive cell value finally is "#¿NAME?". The code is:
For k = pfila_mprov + 1 To ufila_mprov
sn = Sheets(man_prov).Cells(k, 2)
h = Sheets(sn).Index
Select Case k
Case ufila_mprov
suma = suma & " " & Sheets(h).Name & "!" & Sheets(h).Cells(4, 4).Address & ""
Case Else
suma = suma & " " & Sheets(h).Name & "!" & Sheets(h).Cells(4, 4).Address & ","
End Select
Next k
Sheets(pro_pack).Cells(16, 4).Formula = "=sum(" & suma & ")"
End If
i need use the format sheets().cell() to iterate, and finally plus the values saved in "suma" .
Thanks.

Excel VBA Runtime error 1004: Application defined or object defined error

I've scoured the internet for an answer to my problem. I am writing some code to input a formula into certain cells on a worksheet and despite very similar code working perfectly earlier in the macro, this section of code will not work with giving me the runtime error 1004: application-defined or object-defined error.
I have tried moving my code into a new workbook but the problem was not solved and I just can't see why it won't work.
The code below is where I define the sheets I am using
Sub InputFormulae()
Dim wksht As Worksheet
Dim wksht1 As Worksheet
Dim wksht2 As Worksheet
Dim wksht3 As Worksheet
Dim wksht4 As Worksheet
Dim wksht5 As Worksheet
Set wksht = ThisWorkbook.Worksheets("Coils same day remove & insert")
Set wksht1 = ThisWorkbook.Worksheets("Implants same day remove&insert")
Set wksht2 = ThisWorkbook.Worksheets("Implant inserted NO Removal")
Set wksht3 = ThisWorkbook.Worksheets("Implant inserted AND removed")
Set wksht4 = ThisWorkbook.Worksheets("Coil inserted NO removal")
Set wksht5 = ThisWorkbook.Worksheets("Coil inserted AND removed")
The code below is a part of the macro that is working
wksht.Activate
With wksht
i = Range("A" & Cells.Rows.Count).End(xlUp).Row
Do Until i = 1
If .Cells(i, 1) <> "" Then
Cells(i, 9).Formula = "=IF(A" & i & "=A" & i + 1 & ",IF(C" & i & "=C" & i + 1 & ",(H" & i & "-C" & i & "),(F" & i + 1 & "-C" & i & ")),IF(A" & i & "=A" & i - 1 & ",IF(C" & i & "=C" & i - 1 & ",(H" & i & "-C" & i & "),(H" & i & "-C" & i & ")),(H" & i & "-C" & i & ")))"
End If
i = i - 1
Loop
End With
And the code below here is the part that is not working
wksht3.Activate
With wksht3
i = Range("A" & Cells.Rows.Count).End(xlUp).Row
Do Until i = 1
If .Cells(i, 1) <> "" And .Cells(i, 3) <> "" And .Cells(i, 6) <> "" Then
Cells(i, 9).Formula = "=F" & i & "-C" & i & ")"
Else: Cells(i, 9).Value = "0"
End If
i = i - 1
Loop
End With
When I debug the code it highlights the Cells(i, 9).Formula = "=F" & i & "-C" & i & ")" line
Thanks for your time
=F10-C10)
is not a valid formula so you get a 1004
The error you get is because VBA does not understand "=F" & i & "-C" & i & ")". As far as it is a string, the easiest way to debug is to write either:
debug.print "=F" & i & "-C" & i & ")" on the line above and to see the immediate window for the value
or
MsgBox "=F" & i & "-C" & i & ")" on the line above and to see the string in a MsgBox.
Based on the result you would know how to continue.
Start with putting a period in front of every Range and Cells within a With ... End With.
Brackets come in pairs.
Don't turn real numbers into text-that-looks-like-a-number.
wksht3.Activate '<~~ totally unnecessary to use a With ... End With
With wksht3
i = .Range("A" & .Cells.Rows.Count).End(xlUp).Row
Do Until i = 1
If .Cells(i, 1) <> "" And .Cells(i, 3) <> "" And .Cells(i, 6) <> "" Then
.Cells(i, 9).Formula = "=F" & i & "-C" & i
Else
.Cells(i, 9).Value = 0
End If
i = i - 1
Loop
End With
FWIW, you could also just have your formula do the tests too:
With wksht3
i = Range("A" & Cells.Rows.Count).End(xlUp).Row
.Range("I1:I" & i).FormulaR1C1 = "=IF(OR(RC1="""",RC3="""",RC6=""""),0,RC6-RC3)"
End With

Cataloging Problematic Cells by Hyperlink Copying

I'm trying to flag problematic cells and link the cell hyperlink to another cell to review later. Here is my code. Not all of the code is visible. I Called "i" and "j" as long. The error occurs on newLink = Range("AL" & i).Hyperlinks(1).Address claiming that it is "out of range." I think this means that it is calling something that doesn't exist, but to be honest i'm not sure.
If Range("AK" & i).Value = "On" Then
If Range("AL" & i).Value = 0 And Range("AM" & i).Value = 0 Then
Range("AL" & i, "AM" & i).Interior.ColorIndex = 6
'Cells("AL" & i) = H.Address'
ErrorCount = ErrorCount + 1
Dim newLink As String
newLink = Range("AL" & i).Hyperlinks(1).Address
Range("IV" & j).Hyperlinks.Add anchor:=Range("IV" & j), Address:=Range("IV" & j)
Range("IV" & j).Hyperlinks(1).Address = newLink
j = j + 1
End If
If there is no hyperlink attached to a cell then Range("foo").Hyperlinks.Count will return 0 and therefore you will get an 'Out of range' error.
You just need to wrap the newLink = ... statement in an If to check if there is already a hyperlink there. E.g.
If Range("AL" & i).Hyperlinks.Count = 1 Then
newLink = Range("AL" & i).Hyperlinks(1).Address
Else
'what else will you do?
End If

How to do a 3-part IF statement in VBA?

'8
If Not IsEmpty(Range("A7")) And Range("C7") = "\" Then
Range("W7") = "\"
ElseIf Not IsEmpty(Range("A7")) And Range("C7") <> "\" Then
Range("W7") = "\L"
ElseIf IsEmpty(Range("A7")) Then
Range("W7") = ""
End If
With Sheets("DL Data")
.Range("W7:W" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "\L"
End With
I cannot seem to figure out the WITH part, or maybe I am off on the IF statement all together. What I am trying to do here is this:
IF A7 is not empty AND it equals "\" THEN W7 equals "\", also
IF A7 is not empty AND it does not equal "\" THEN W7 equals "\L", also
IF A7 is empty THEN W7 equals blank
Then I would like it to do this to search through the Column and give me the appropriate answer, inserting blanks where necessary, inserting "\" where necessary and inserting "\L" where necessary.
Hope I am making myself clear here, thanks for all of the help in advance!
Regards,
Yazz
The With is simply an abbreviation.
For example
With Sheets("DL Data")
.Range("W7:W" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "\L"
End With
Is equivalent to
Sheets("DL Data").Range("W7:W" & Sheets("DL Data").Cells(Sheets("DL Data").Rows.Count, "A").End(xlUp).Row).Formula = "\L"
You need a loop in order to apply this condition to all of the rows, for example:
Dim i as Integer
For i = 7 To Sheets("DL Data").Range("A7").End(xlDown).Row
If Not IsEmpty(Range("A" & i)) And Range("C" & i) = "\" Then
Range("W" & i) = "\"
ElseIf Not IsEmpty(Range("A" & i)) And Range("C" & i) <> "\" Then
Range("W" & i) = "\L"
ElseIf IsEmpty(Range("A" & i)) Then
Range("W" & i) = ""
End If
Next i
If you want to run it through the whole column, you need to nest the same If-block into a loop:
With Sheets("DL Data")
For j = 7 To .Range("A7").End(xlDown).Row
If .Range("A" & j) <> "" And .Range("C" & j) = "\" Then
.Range("W" & j) = "\"
ElseIf .Range("A" & j) <> "" And .Range("C" & j) <> "\" Then
.Range("W" & j) = "\L"
ElseIf .Range("A" & j) = "" Then
.Range("W" & j) = ""
End If
Next j
End With
WARNING I have followed your current implementation; however, if your column "A" might have some empty cells in between (which would explain the presence of a ElseIf .Range("A" & j) = ""), pay attention to the fact that the counter .Range("A7").End(xlDown).Row would stop at the first empty cell. You might want to index your counter into an always filled-up column (such as a record-ID of your data set).
This update to your code will update the W column according to your formula. I optimized the If statements a bit to make them less redundant.
Additionally, I have included a couple of With blocks for illustration purposes.
With Sheets("DL Data")
Dim row As Long
For row = 7 To .Cells(.Rows.Count, "A").End(xlUp).Row)
With .Range("A" & row)
' IF A is empty THEN W equals blank
If .Value = "" Then
Range("W" & row).Value = ""
' IF A is not empty AND it equals "\" THEN W equals "\"
If .Value = "\" Then
Range("W" & row).Value = "\"
' IF A is not empty AND it does not equal "\" THEN W equals "\L"
' A is not empty (first condition checked above).
ElseIf .Value <> "\"
Range("W" & row).Value = "\L"
End If
End With
Next
End With

VB.net .tostring("0.000") from datagridview

I am really stuck on this. I have used numeric string formatting before, but not out of a datagridview. Any time I try to format a number to "0.000" string from the datagridview it does nothing, just returns the number in the cell unformatted. Can anyone help correct this?
For Each manualprocess In ManualAddOperationsdgv.Rows
If manualprocess.cells(2).value.ToString = "DRILL" Then
If lasttool <> manualprocess.cells(4).value.ToString Or lastoffset <> manualprocessoffset Or lastdepth <> manualprocess.cells(10).value Then
If Not lasttool = 0 Then
builder.AppendLine("M9M5M1")
End If
builder.AppendLine("")
builder.AppendLine("N" & blocknumber & "(" & manualprocess.cells(1).value.ToString & " - DRILL OPERATION)")
blocknumber += 1
builder.AppendLine("G65P7000T" & manualprocess.cells(4).value.ToString & "M6B#.S" & manualprocess.cells(12).value.ToString & "CC#." & manualprocessoffset & "(D#)")
builder.AppendLine("G66P7015E" & manualprocess.cells(3).value.ToString & "R.1Q" & manualprocess.cells(14).value.ToString & "Z-" & manualprocess.cells(10).value.ToString("0.0000") & "S" & manualprocess.cells(12).value.ToString & "F" & manualprocess.cells(13).value.ToString("0.0000") & manualprocessoffset)
End If
builder.AppendLine("X" & manualprocess.cells(8).value.ToString("0.000") & "Y" & manualprocess.cells(9).value.ToString("0.000"))
End If
Next
The ToString function doesn't know what type of object is in your cell, so try converting your value into a decimal or a double first:
Dim decValue As Decimal
If Decimal.TryParse(manualprocess.Cells(8).Value.ToString, decValue) Then
builder.AppendLine("X" & decValue.ToString("0.000") ...
End If