I am trying to reformat a report so it can feed into my system like below:
wbOutput.Sheets(1).Range("B" & O_lrow + 1 & ":B" & O_lrow + lRow).Value = wbSource.Sheets(1).Range("F1:F" & lRow).Value
One issue I encounter is column F needs to be the sum of two source column and below doesn't work:
wbOutput.Sheets(1).Range("F" & O_lrow + 1 & ":F" & O_lrow + lRow).Value = wbSource.Sheets(1).Range("N1:N" & lRow).Value + wbSource.Sheets(1).Range("O1:O" & lRow).Value
I am trying to avoid using loop as there are many rows and I don't want the marco slow down too much.
Is there any simple way to achieve this without using a loop?
You can try this:
wbOutput.Sheets(1).Range("F" & O_lrow + 1 & ":F" & O_lrow + lRow).Value = _
wbSource.Sheets(1).Evaluate("N1:N" & lRow & " + O1:O" & lRow)
This is a way, using the Application.Sum function:
Option Explicit
Sub SumTest()
Dim SumA As Range
Dim SumB As Range
With wbSource.Sheets(1)
Set SumA = .Range("N1:N" & lRow)
Set SumB = .Range("O1:O" & lRow)
End With
wbOutput.Sheets(1).Range("F" & O_lrow + 1 & ":F" & O_lrow + lRow) = Application.Sum(SumA, SumB)
End Sub
You already have two good answers, just want to add my 2 cents here...
If you have lots of data, you should consider using arrays, and one way of doing what you are trying to achieve can be the below, please see comments for further details:
Dim wsOutput As Worksheet: Set wsOutput = wbOutput.Sheets(1) 'allocate the output worksheet to a variable
Dim wsSource As Worksheet: Set wsSource = wbSource.Sheets(1) 'allocate the source worksheet to a variable
Dim arrSource As Variant
Dim arrOutput() As Variant 'Could change this to match your expected data type output
Dim R As Long, C As Long
arrSource = wsSource.Range("N1:O" & lRow) 'Get the source data into an array
ReDim arrOutput(1 To UBound(arrSource), 1 To 1) 'set the size of the output
For R = 1 To UBound(arrSource)
For C = 1 To UBound(arrSource, 2)
arrOutput(R, 1) = arrSource(R, 1) + arrSource(R, 2) 'make your calculations here
Next C
Next R
wsOutput.Range("F" & O_lrow + 1 & ":F" & O_lrow + lRow) = arrOutput 'spit it back to the sheet once is done
Related
After the vba macro is running only the values sholud be visible into the cells. In addtional all special character #N/A" should be removed. Everywhere where #N/A stands should then be an empty field.
Dim sh As Worksheet, shOld As Worksheet, shNew As Worksheet, lastR As Long, rngB As Range
Dim rngBJ As Range, rngBN As Range, lastR2 As Long, lastR3 As Long, arrVlk, iRow As Long, i As Long, l As Long
iRow = 5 'the row where from the data will be returned
Set sh = Worksheets("PIV Kunde SO & Status")
Set shOld = Worksheets("oldStockAge")
Set shNew = Worksheets("PIV Kunde SO, Vendor & Age")
lastR = sh.Range("B" & sh.Rows.Count).End(xlUp).Row
lastR2 = shOld.Range("B" & sh.Rows.Count).End(xlUp).Row
lastR3 = shNew.Range("B" & sh.Rows.Count).End(xlUp).Row
Set rngB = sh.Range("B" & iRow & ":B" & lastR)
Set rngBJ = shOld.Range("B5:J" & lastR2)
Set rngBN = shNew.Range("B2:F" & lastR3)
For l = 2 To 6
sh.Cells(iRow, l + 2).Formula = "=VLOOKUP(B5," & rngBN.Address(external:=True) & "," & l & ",0)"
Next l
sh.Range("D" & iRow, "F" & iRow).AutoFill Destination:=sh.Range("D" & iRow, "F" & lastR)
For i = 7 To 9
sh.Cells(iRow, i + 1).Formula = "=VLOOKUP(B5," & rngBJ.Address(external:=True) & "," & i & ",0)"
Next i
sh.Range("D" & iRow, "I" & iRow).AutoFill Destination:=sh.Range("D" & iRow, "I" & lastR)
Please, add to the end of your existing code the next lines:
Dim rngNA as Range
On Error Resume Next 'just to avoid a code error in case of no #N/A return...
set rngNa = sh.Range("D" & iRow, "I" & lastR).SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rngNA Is Nothing Then rngNA.Value = ""
Or try adapting the formula to return an empty string in case of no match, in the next way:
Dim strFormula As String
For l = 2 To 6
strFormula = "VLOOKUP(B5," & rngBN.Address(external:=True) & "," & l & ",0)"
sh.cells(iRow, l + 4).Formula = "=If(ISNA(" & strFormula & "),""""," & strFormula & ")"
Next l
strFormula is used only to avoid a big 'sausage' formula... :)
I have excel sheet with around 40k records and 5 columns. I want to search duplicates in column 3, 4, 5 and copy whole row in new sheet.
#Emm Jay could you please be more specific? I m not sure what are you asking for, but the below code may help you to get an overall idea.
Let's say that Sheet 1 contains our data & duplicate rows will copy on Sheet 2.
Sheet 1:
Sheet 2 - Output:
Code:
Option Explicit
Sub Duplicates()
Dim LastrowS1 As Long, LastrowS2 As Long, i As Long, j As Long
Dim CombineStrI As String, CombineStrJ As String
LastrowS1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastrowS1
CombineStrI = Sheet1.Range("C" & i).Value & "_" & Sheet1.Range("D" & i).Value & "_" & Sheet1.Range("E" & i).Value
For j = 2 To LastrowS1
CombineStrJ = Sheet1.Range("C" & j).Value & "_" & Sheet1.Range("D" & j).Value & "_" & Sheet1.Range("E" & j).Value
If j <> i Then
If CombineStrI = CombineStrJ Then
Sheet1.Rows(i).Copy
LastrowS2 = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
Sheet2.Range("A" & LastrowS2 + 1).PasteSpecial
End If
End If
Next j
Next i
End Sub
Please can you help
I am trying to use the autosum feature in the highlighted boxes as per the example below (these are shown in yellow for illustration purposes only)
In column A the word "Total" appears and this is the indication that the autosum needs to be used in columns L,M & N
The data in each section varies in length so unfortunately I am not able just to record a macro to achieve this.
I need to use VBA coding, so if you are able to help it would be greatly appreciated.
Thanks
Regards
Steve
Dim ws As Excel.Worksheet
Dim lRow As Long
Dim lStartSection As Long
Set ws = Application.Sheets("Sheet1")
lRow = 3
lStartSection = 0
'Loop through the used range in the spreadsheet
Do While lRow <= ws.UsedRange.Rows.Count
'If we are at the start of a section record the row.
If lStartSection = 0 And ws.Range("A" & lRow).Value <> "" Then
lStartSection = lRow
End If
'Check if we are at the bottom of a section
If ws.Range("A" & lRow).Value = "Total" Then
ws.Range("L" & lRow).Value = "=SUM(L" & lStartSection & ":L" & lRow - 1 & ")"
ws.Range("M" & lRow).Value = "=SUM(M" & lStartSection & ":M" & lRow - 1 & ")"
ws.Range("N" & lRow).Value = "=SUM(N" & lStartSection & ":N" & lRow - 1 & ")"
'Set this back to zero so we know that we are no longer in a section
lStartSection = 0
End If
lRow = lRow + 1
Loop
I have a sheet:
I am trying to write code to be able to combine multiple values into one row, I need to sum the values from columns, B, C and D.
My aim is to be able to press a button and I have all of my duplicate values removed, but before this, the numerical values in the adjacent columns are summed into the single version.
So far I have removed the duplicates from the column:
Sheets("Sheet4").Select
With Columns("A:A")
.Replace What:="mobile", Replacement:=""
End With
Previous code should do your job. It may need a fine tuning but idea would work. Do not forget to make proper addressing of worksheets for your ranges. I did not do it. This will work on the active sheet currently.
Update: Updated with worksheet addresses.
Dim ws As Worksheet
Dim LastRow As Long
Dim S_Value As String
Set ws = Sheets("Sheet1")
LastRow = Range("A" & Rows.Count).End(xlUp).Row
i = 2
While i <= LastRow
S_Value = ws.Range("A" & i).Value
j = i + 1
While j <= LastRow
If ws.Range("A" & j).Value = S_Value Then
ws.Range("B" & i).Value = ws.Range("B" & i).Value + ws.Range("B" & j).Value
ws.Range("C" & i).Value = ws.Range("C" & i).Value + ws.Range("C" & j).Value
ws.Range("D" & i).Value = ws.Range("D" & i).Value + ws.Range("D" & j).Value
ws.Rows(j & ":" & j).EntireRow.Delete
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
j = j - 1
End If
j = j + 1
Wend
i = i + 1
Wend
Here you go,
Sub SumCount()
Dim s, c, sm
Dim Rws As Long, Rng As Range
Rws = Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range(Cells(2, 2), Cells(Rws, 4))
s = InputBox("What Number to Find?")
c = Application.WorksheetFunction.CountIf(Rng, s)
sm = s * c
MsgBox sm
End Sub
I have a problem making a little VBA to copy/paste some datas. I looked around and didn't really find any post who talk of my problem.
Here is my problem: I have 3 worksheets who need to be copied on a fourth worksheet. Each worksheet have between 200 and 650 lines. On the three sheets, it's the columns A, I, J, K, L, M,N who need to be copied on the columns A, C, D, H, I, M, N. The copy paste action need to start on the first blank line of the fourth sheet. This is the last constraint who make it a lot more difficult than I expected. I tried two ways and haven't managed to make it works.
Here is the code (one way is in comments form)
Dim Sh as Worksheet
Dim i as Integer
For Each Sh In Sheets(Array("Janvier", "Février", "Mars"))
For i = 4 To 650
Worksheets("Sh").Range("A & i").Copy Destination:=Worksheets("Calculs").Range("A" & Sheets("Calculs").UsedRange.Rows.Count + 1)
Worksheets("Sh").Range("I & i:J & i").Copy Destination:=Worksheets("Calculs").Range("I" & Sheets("Calculs").UsedRange.Rows.Count + 1)
Worksheets("Sh").Range("K & i:L & i").Copy Destination:=Worksheets("Calculs").Range("K" & Sheets("Calculs").UsedRange.Rows.Count + 1)
Worksheets("Sh").Range("M & i:N & i").Copy Destination:=Worksheets("Calculs").Range("M" & Sheets("Calculs").UsedRange.Rows.Count + 1)
'Sheets("Calculs").Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("A4:A650").Value
'Sheets("Calculs").Range("C" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("I4:J650").Value
'Sheets("Calculs").Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("K4:L650").Value
'Sheets("Calculs").Range("M" & Rows.Count).End(xlUp).Offset(1).Value = Sheets(Sh).Range("M4:n650").Value
Next i
Next Sh
My error after executing the code not in comments form is "Subscript out of range". Can you propose me a better way to code this.
Thank you for your help, Olivier
Try using the .Cells method instead of .Range. Like so:
Worksheets("Sh").Cells(i, 1) ...
Where the first parameter is your row and the second is your columns (A=1, B=2, ect).
Try this:
Sub Tester()
Dim Sh As Worksheet, ws As Worksheet, rw As Range
Dim i As Integer
Set ws = Worksheets("Calculs")
'get first empty row
Set rw = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
Application.ScreenUpdating = False
For Each Sh In Sheets(Array("Janvier", "Février", "Mars"))
For i = 4 To 650
Sh.Range("A" & i).Copy rw.Cells(1, "A")
Sh.Range("I" & i & ":J" & i).Copy rw.Cells(1, "I")
Sh.Range("K" & i & ":L" & i).Copy rw.Cells(1, "K")
Sh.Range("M" & i & ":N" & i).Copy rw.Cells(1, "M")
Set rw = rw.Offset(1, 0)
Next i
Next Sh
End Sub