I have the following vba code, but I want to paste it giving the format of dd-mm-yyyy.
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
I've tried:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr.Numberformat = ('dd-mm-yyyy')
I am unsure on the format of this. Where do I put numberformat?
on a different line, two actions:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).NumberFormat = "dd-mm-yyyy"
But we can shorten it a little with With and Resize
With Worksheets("stack").Range("M" & LastRowM + 1).resize(Ubound(pasteArr,1),18)
.Value = PasteArr
.NumberFormat = "dd-mm-yyyy"
End With
Related
I have the following issue: I create a chart from values that a user will insert in specific cells of a spreadsheet and the chart displays what I expect (The series are collected in the right way). The Series are created consecutively with 2 data points only. I want to store the Xvalues (And YValues) of the series in dynamic arrays for performing calculations later (outside the series collection loop). I tried the following but the arrays only store the last values of the Series (despite using redim preserve everywhere).
Is it because the array is not redimensioned correctly?
Thanks for your help!
i = 1
SeriesAddedA = 1
RangeOccupiedA = Range("A2").End(xlDown).Row
RangeOccupiedB = Range("B2").End(xlDown).Row
MaxRangeOccupied = WorksheetFunction.Max(RangeOccupiedA, RangeOccupiedB)
Dim XValuesA()
Dim YValuesA()
ReDim XValues(RangeOccupiedA + 2)
ReDim YValues(RangeOccupiedA + 2)
Do While i < MaxRangeOccupied
FilledCompA = NextFilled(Cells(i, 1))
PrevCell = PrevFilled(Cells(i + 1, 1))
rowdiff = FilledCompA - PrevCell
If rowdiff < 2 And PrevCell <> 0 And FilledCompA <> 0 Then
ReDim Preserve XValuesA(1 To RangeOccupiedA + 2)
chart.SeriesCollection.NewSeries
chart.FullSeriesCollection(SeriesAddedA).Name = "Reaction Step A " & SeriesAddedA - 1
chart.FullSeriesCollection(SeriesAddedA).XValues = "=" & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA - 1 & ":$E$" & FilledCompA
chart.FullSeriesCollection(SeriesAddedA).Values = "=" & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA - 1 & ":$M$" & FilledCompA
XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues
SeriesAddedA = chart.SeriesCollection.Count + 1
ElseIf PrevCell <> 0 And rowdiff > 0 Then
ReDim Preserve XValuesA(1 To RangeOccupiedA + 2)
chart.SeriesCollection.NewSeries
chart.FullSeriesCollection(SeriesAddedA).Name = "Reaction Step prev A " & SeriesAddedA - 1
chart.FullSeriesCollection(SeriesAddedA).XValues = "=" & "(" & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA - rowdiff & "," & "'" & ActiveSheet.Name & "'" & "!$E$" & FilledCompA & ")"
chart.FullSeriesCollection(SeriesAddedA).Values = "=" & "(" & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA - rowdiff & "," & "'" & ActiveSheet.Name & "'" & "!$M$" & FilledCompA & ")"
XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues
SeriesAddedA = chart.SeriesCollection.Count + 1
End If
loop
'the debug print says:
XvaluesA(1) 310.52
XvaluesA(2) 408.58
With XValuesA() = chart.SeriesCollection(SeriesAddedA).XValues you overwrite the data in XValuesA everytime you call it in your loop. That is why it only contains the last values.
Use XValuesA(i) = chart.SeriesCollection(SeriesAddedA).XValues instead and then
Debug.Print XvaluesA(1)(1)
Debug.Print XvaluesA(1)(2)
gives the first values and
Debug.Print XvaluesA(2)(1)
Debug.Print XvaluesA(2)(2)
the second …
I have written a block of code using VBA in excel that counts the number of records produced in a file exported from another platform on the very last line of the file.
This file has to contain a specific number of characters to validate. If the record count is 9 or less then it will operate just fine. However, if the record count is 10 or above the character limit is exceeded and it will fail.
Is there a way to embed a line code into the module that will pad the output from this last line of syntax with a leading 0 if the record count is less than 10? If so, I can then alter the earlier part of the code to remove a static character and meet the files requirements regardless of if the record count is above or below 9.
Code is below...The bolded area on the last line of the code at the very end (" & lastrow - 2 & ") is where I need to pad the 0 of the resulting formula.
Sub Hidden_macro1()
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Testing_Report_Out").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Sheets.Add.Name = "Testing_Report_Out"
Dim lastrow As Long
Dim lastrowfooter As Long
Dim I As Integer
Dim J As Integer
Sheets("Testing Report").Select
lastrow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
Sheets("Testing_Report_Out").Select
Range("A1").Formula = "=CONCATENATE('Testing Report'!A4,"" "",'Testing Report'!B4,'Testing Report'!C4,"" "",'Testing Report'!D4,'Testing Report'!E4,"" "",'Testing Report'!F4)"
I = 4
J = 2
Do Until I = lastrow
'Fill 1st formula (:20:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!G" & I & ",'Testing Report'!H" & I & ")"
J = J + 1
'Fill 2nd formula (:23B:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!I" & I & ",'Testing Report'!J" & I & ")"
J = J + 1
'Fill 3rd formula (:32A:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!K" & I & ",'Testing Report'!L" & I & ",'Testing Report'!M" & I & ",'Testing Report'!N" & I & ",'Testing Report'!O" & I & ")"
J = J + 1
'Fill 4th formula (:50K:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!P" & I & ",'Testing Report'!Q" & I & ")"
J = J + 1
'Fill 5th formula (:50K: Addy)
Range("A" & J).Formula = "='Testing Report'!R" & I & ""
J = J + 1
'Fill 6th formula (:57A:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!S" & I & ",'Testing Report'!T" & I & ")"
J = J + 1
'Fill 7th formula (:59:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!U" & I & ",'Testing Report'!V" & I & ")"
J = J + 1
'Fill 8th formula (:59:/ 3)
Range("A" & J).Formula = "='Testing Report'!W" & I & ""
J = J + 1
'Fill 9th formula (:59:/ 5)
Range("A" & J).Formula = "='Testing Report'!X" & I & ""
J = J + 1
'Fill 10th formula (:70:)
Range("A" & J).Formula = "=CONCATENATE('Testing Report'!Z" & I & ",'Testing Report'!AA" & I & ")"
J = J + 1
'Fill 11th formula (:71A:)
Range("A" & J).Formula = "='Testing Report'!AB" & I & ""
J = J + 1
'Fill 12th formula (:72:)
Range("A" & J).Formula = "='Testing Report'!AC" & I & ""
I = I + 1
J = J + 1
Loop
lastrowfooter = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Range("A" & lastrowfooter + 1).Formula = "=CONCATENATE('Testing Report'!AD4,'Testing Report'!AE4,'Testing Report'!AF4,'Testing Report'!AG4,"" "",'Testing Report'!AH4,'Testing Report'!AI4,'Testing Report'!AJ4," **& lastrow - 2 &** ")"
End Sub
You can use Format() as suggested by #BigBen
Range("A" & lastrowfooter + 1).Formula = _
"=CONCATENATE('Testing Report'!AD4,'Testing Report'!AE4,'Testing Report'!AF4," & _
"'Testing Report'!AG4,"" "",'Testing Report'!AH4,'Testing Report'!AI4," & _
"'Testing Report'!AJ4," & Format(lastrow - 2, "00") & ")"
In the following code I would like to delete the char 160 and 34 characters at the end of the text string.
For Column = 12 To 13
For i = 16 To last_row
'sString = ("=CODE(160)")
'Range(Cells(i, Column), Cells(i, Column)).Select
'Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Value = Replace(Range(Cells(i, Column), Cells(i, Column)).Value, sString, "", 1, -1)
Set current_cell = Range(Cells(i, Column), Cells(i, Column))
current_cell.Select
text_to_change = Range(Cells(i, Column), Cells(i, Column)).Value
Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Select
Selection.NumberFormat = "General"
With Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column))
.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")"
.Value = .Value
End With
Next i
Next Column
I'm getting an error at this line:
.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")".
The error message indicates:
Run-time error '1004': Application-defined or object-defined error.
Could you help me what is wrong here?
You need to escape your quote marks:
.Formula = "=SUBSTITUTE(""" & text_to_change & """;CODE(160);""" & Chr(34) & Chr(34) & """)"
so what i want is when user add a product which is already registered in the sheet, the quantity of that product is added and not add new row. this is the code that i already make but the problem with this code is that the system doesnt enter the if loop and so it adds new rows again.
this is the sample:
code:
Private Sub btnSubmit_Click()
Sheet2.Activate
Dim lastRow2 As Long
lastRow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
Set rng2 = Worksheets("Sheet2").Range("B2:B" & lastRow2)
For Each cell2 In rng2
If cell2 = tbTipe.Text + " " + "(" + tbColor.Text + ")" + " " + "-" + " " + tbProduct.Text Then
cell2.Offset(0, 1) = cell2.Offset(0, 1) + tbQty.Value
Else
Sheets("Sheet2").Range("A" & lastRow2).Value = CDate(tbDate)
Sheets("Sheet2").Range("B" & lastRow2).Value = UCase(tbTipe.Text + " " + "(" + tbColor.Text + ")" + " " + "-" + " " + tbProduct.Text)
Sheets("Sheet2").Range("C" & lastRow2).Value = tbQty.Value
Sheets("Sheet2").Range("D" & lastRow2).Value = tbPrice.Value
End If
Next cell2
End Sub
Private Sub UserForm_Initialize()
tbDate.Value = Date
tbProduct.Value = ""
tbQty.Value = ""
tbPrice.Value = ""
tbTipe.Value = ""
tbColor.Value = ""
End Sub
Generate your product string and look for it in column B. If found, add the quantity to column C; if not add a new row with all information.
Private Sub btnSubmit_Click()
Dim lastRow2 As Long, m as variant, str as string
with workSheets("Sheet2")
.Activate
str = UCase(tbTipe.Text & " (" & tbColor.Text & ") - " & tbProduct.Text)
m = application.match(str, .range("B:B"), 0)
if not iserror(m) then
.Range("C" & m).Value = .Range("C" & m).Value + val(tbQty.Value)
else
lastRow2 = .Range("A" & Rows.Count).End(xlUp).Row + 1
.Range("A" & lastRow2).Value = CDate(tbDate)
.Range("B" & lastRow2).Value = str
.Range("C" & lastRow2).Value = val(tbQty.Value)
.Range("D" & lastRow2).Value = val(tbPrice.Value)
End If
Next cell2
End Sub
VBA's string concatenation symbol is the ampersand (&), not the plus sign (+).
Can someone help me get the correct formula?
I want to calculate the following:
SQRT((Rng2^2) + (Rng4^2))
Here is my code but it is not working
Cells(LastRow + 1, LastCol + 4) = "=SQRT((Rng2.Address)^2 & " + " & (Rng4.Address)^2)"
Your concatenation is in the wrong places:
Cells(LastRow + 1, LastCol + 4) = "=SQRT((" & Rng2.Address & ")^2 + (" & Rng4.Address & ")^2)"