VBA while-looping with for/if conditions - vba

I'm working on this macro to extract the relavant information from an excel sheet to a new one. But i just can't seem to get it to work and it's making me crazy.
Can you find the error or suggest a better approch?
EDIT: I want to sort through the data in a sheets column c and if it matches my critera (if) i want to match it to the Group (fruit/berry) and also get data from adjecent columns (P,I,R) and take this whole extraxt and add it to a new sheet (ws2)
Private Sub Extract_Click()
Dim ws1, ws2 As Worksheet
Dim i,k as Integer
set ws1 = Workbook.Activesheet
Set ws2 = Worksheets.Add(After:= _
Worksheets(ThisWorkbook.Sheets.Count))
ws2.Name = "Extract" & ThisWorkbook.Sheets.Count
With ws1
i = 10
k = 2
Do While Not Range("C" & i).Value = ""
If Range("C" & i).Value = "Strawberry" Then
ws1.Range("C" & i).Copy
ws2.Range("A" & k).PasteSpecial Paste:=xlPasteValues
ws2.Range("B" & k).Value = "Berry"
ws1.Range("P" & i).Copy
ws2.Range("C" & k).PasteSpecial Paste:=xlPasteValues
ws1.Range("I" & i).Copy
ws2.Range("D" & k).PasteSpecial Paste:=xlPasteValues
ws1.Range("R" & i).Copy
ws2.Range("E" & k).PasteSpecial Paste:=xlPasteValues
ElseIf Range("C" & i).Value = "banana" Then
ws1.Range("C" & i).Copy
ws2.Range("A" & k).PasteSpecial Paste:=xlPasteValues
ws2.Range("B" & k).Value = "Fruit"
ws1.Range("P" & i).Copy
ws2.Range("C" & k).PasteSpecial Paste:=xlPasteValues
ws1.Range("I" & i).Copy
ws2.Range("D" & k).PasteSpecial Paste:=xlPasteValues
ws1.Range("R" & i).Copy
ws2.Range("E" & k).PasteSpecial Paste:=xlPasteValues
End If
k = k+1
i = i +1
Loop
End With
End Sub

I would suggest using a For Loop first of all, and then checking if the cell values match something like:
For i = 2 to lastRow
If Cell(i, columnNumber).value = Cell(i, columnNumber).Value Then
//Do Something

Related

VBA wait until another excel file is fully open

Spending some time with my code I realized I need your help.
The problem is following. I have a file with VBA code in it (ThisWorkbook) and i'm trying to open another excel book to copy data from three different sheets there and paste into my excel in thee different sheets either. Copying the data from first two sheets goes flawlessly but there is problem with the third one.
The interest thing is that when I try do my job in debug mode, everything goes right, but when i run macro the problem exists.
Here is my code:
Option Explicit
Const NORMAT_START_POINT = 3 'data starts from this row after header
Const PA_MAN_ADJ_START_POINT = 9
Sub copy_paste_data_to_checklist()
Dim FileToOpen As Variant
Dim WorkingFile As Workbook
Dim countCompanies As Long
Dim period As Variant 'month and year for copy paste the table header
Dim no_sent_reports_count As Long
countCompanies = licenses.Range("A1").End(xlDown).Row
period = fzp_nzp_pa.Range("Q1").Value
'On Error GoTo handle
no_sent_reports_count = VBA.InputBox("How many companies didn't provide reports for
previous quarter?", "Enter data")
'On Error GoTo 0
Call EntryPoint
FileToOpen = Application.GetOpenFilename(FileFilter:="Excel files(*.xlsb), *.xlsb", Title:="Select a file to import data from")
'clear data before pasting new values
fzp_nzp_pa.Range("A3", "M" & countCompanies + NORMAT_START_POINT).ClearContents
nro_nja_normat.Range("A3", "L" & countCompanies + NORMAT_START_POINT).ClearContents
'open file with nornativ
'''''''''''''''''''''''''''''''''''''''''''''copy paste fzp_nzp_pa'''''''''''''''''''''''''''''''''''''''
If FileToOpen <> False Then
Set WorkingFile = Workbooks.Open(FileToOpen)
WorkingFile.Application.Calculation = xlCalculationManual
If isWorkbookOpen(WorkingFile.Name) = True Then
'Application.Wait (Now + TimeValue("0:00:30"))
WorkingFile.Worksheets("Íîðìàòèâû").Range("A3", "B" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("A3").PasteSpecial xlPasteValues
'fzp_nzp
WorkingFile.Worksheets("Íîðìàòèâû").Range("F3", "G" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("C3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("K3", "K" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("E3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("Q3", "Q" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("F3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("V3", "V" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("G3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("M3", "N" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("H3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("Z9", "Z" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
fzp_nzp_pa.Range("J3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AO9", "AO" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
fzp_nzp_pa.Range("K3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AV9", "AV" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
fzp_nzp_pa.Range("L3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("S3", "S" & countCompanies + NORMAT_START_POINT).Copy
fzp_nzp_pa.Range("M3").PasteSpecial xlPasteValues
'''''''''''''''''''''''''''''''''''''''''copy paste nro_nja_normat'''''''''''''''''''''''''''''''''''''''
WorkingFile.Worksheets("Íîðìàòèâû").Range("A3", "B" & countCompanies + NORMAT_START_POINT).Copy
nro_nja_normat.Range("A3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AA9", "AA" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("C3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AB9", "AB" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("D3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AC9", "AC" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("E3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AF9", "AF" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("F3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AF9", "AF" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("F3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AG9", "AG" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("G3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AL9", "AL" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("H3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AP9", "AP" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("I3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AS9", "AS" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("J3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AT9", "AT" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("K3").PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AU9", "AU" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_normat.Range("L3").PasteSpecial xlPasteValues
''''''''''''''''''''''''''''''''''''''''copy paste to nro_nja_archive''''''''''''''''''''''''''''''''''''
'copy the header
nro_nja_archive.Range("A2:S2").Copy
'paste it 5 rows below the last found non-empty cell
nro_nja_archive.Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 5 + no_sent_reports_count).PasteSpecial xlPasteAllUsingSourceTheme
'insert period date above the header in column A
fzp_nzp_pa.Range("Q1").Copy
nro_nja_archive.Range("A" & Range("A" & Rows.Count).End(xlUp).Row - 1).PasteSpecial xlPasteValues
'nro_nja_archive.Range("A" & Range("A" & Rows.Count).End(xlUp).Row - 1).Value = period
nro_nja_archive.Range("A" & Range("A" & Rows.Count).End(xlUp).Row - 1).Interior.Color = VBA.RGB(158, 28, 176)
nro_nja_archive.Range("A" & Range("A" & Rows.Count).End(xlUp).Row - 1).Font.Color = vbWhite
'copy paste data to nro_nja_archive
WorkingFile.Worksheets("Íîðìàòèâû").Range("A3", "B" & countCompanies + NORMAT_START_POINT).Copy
nro_nja_archive.Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AO9", "AO" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("C" & Range("C" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("Q3", "Q" & countCompanies + NORMAT_START_POINT).Copy
nro_nja_archive.Range("D" & Range("D" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AB9", "AB" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("E" & Range("E" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AF9", "AF" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("F" & Range("F" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AA9", "AA" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("G" & Range("G" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AL9", "AL" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("H" & Range("H" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AG9", "AG" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("I" & Range("I" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AC9", "AC" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("J" & Range("J" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AV9", "AV" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("K" & Range("K" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("V3", "V" & countCompanies + NORMAT_START_POINT).Copy
nro_nja_archive.Range("L" & Range("L" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AP9", "AP" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("M" & Range("M" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AS9", "AS" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("N" & Range("N" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AU9", "AU" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("O" & Range("O" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("ÏÀ_manual_adj").Range("AT9", "AT" & countCompanies + PA_MAN_ADJ_START_POINT).Copy
nro_nja_archive.Range("P" & Range("P" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Worksheets("Íîðìàòèâû").Range("W3", "Y" & countCompanies + NORMAT_START_POINT).Copy
nro_nja_archive.Range("Q" & Range("Q" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
WorkingFile.Close False
MsgBox "Done!", vbInformation, "Task completed"
End If
End If
Call ExitPoint
' Exit Sub
'handle:
End Sub
Sub EntryPoint()
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.DisplayAlerts = False
.StatusBar = "Winter is coming"
End With
End Sub
Sub ExitPoint()
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
.CutCopyMode = False
.StatusBar = ""
End With
End Sub
Function isWorkbookOpen(WorkbookName As String) As Boolean
Dim wb As Workbook
On Error Resume Next
Set wb = Application.Workbooks(WorkbookName)
On Error GoTo 0
If wb Is Nothing Then
isWorkbookOpen = False
Else
isWorkbookOpen = True
End If
End Function
The problem starts from this block :
''''''''''''''''''''''''''''''''''''''''copy paste to nro_nja_archive''''''''''''''''''''''''''''''''''''
It should take data from opened file and insert it below the last rows with data. It seems like file isn't loaded fully when the code executes, since data are being pasted anywhere but not below the last row. Again, when in debug mode everything is working just fine... I need somehow make the vba code wait until another excel file is fully loaded before performing copy paste.
Any help will be much appreciated!

VBA Adding a button that adds a row that copies the formulas in a certain from above the activecell, not working

I want to make a button that copies a certain formulas within a range, and inserts it below the activecell.
It works, but now the formula reference keeps linking to the formula above it, I need it to act like an AutoFill,
Sub Rijen_Toevoegen()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
Range("B" & ActiveCell.Row + 1 & ":H" & ActiveCell.Row + 1).Formula = Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row).Formula
End Sub
You can use autofill like this:
Sub Rijen_Toevoegen()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row).AutoFill Destination:=Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row + 1)
End Sub
Look that the destination range must include the ActiveCell's row "B" & ActiveCell.Row and also the row you are filling ":H" & ActiveCell.Row + 1 (plus 1)
Let me know if it works
Offset should be used like this:
currentSheet.yourRange.Offset(offsetRow, offsetColumn)
With that, you should change your code to:
Sub Rijen_Toevoegen()
ActiveCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown
Range("B" & ActiveCell.Row + 1 & ":H" & ActiveCell.Row + 1).Formula = Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row).Formula
End Sub
Also, it is recommended to avoid using .Select, .Active, ActiveCell and the likes.

vba vlookup fill to last row

I am new to this VBA and need some help with my code. I manage to get my code to vlookup from last row in column O but I dont know how to fill it to match last row of column E.
My goal is vlookup from last row of O fill to last row of E
Dim JPNpart, PartNumber, myRange, LastRow As Long
LastRow = Range("E" & Rows.Count).End(xlUp).Row
JPNpart = "[JPN_part.xlsx]Sheet1"
Sheets("Sheet1").Select
Range("O2").Select
Selection.End(xlDown).Offset(1, 0).Select
PartNumber = ActiveCell.Offset(0, -13).Address
myRange = "'" & JPNpart & "'!A:G"
Range("O2").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 7, FALSE)"
'how i do to make this formula fill till last row
Range("P2").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 2, FALSE)"
Range("E2").Select
Selection.End(xlDown).Select
Thanks for your help.
You could have figured it out by cleaning the Select/Selection and Activate/ActiveCell.
Here is your code cleaned of that and made more understable :
Dim JPNpart As String, PartNumber As String, myRange As String, LastRow As Long
JPNpart = "[JPN_part.xlsx]Sheet1"
myRange = "'" & JPNpart & "'!A:G"
With ThisWorkbook.Sheets("Sheet1")
'For column O
LastRow = .Range("O" & .Rows.Count).End(xlUp).Row
PartNumber = "B" & LastRow + 1
.Range("O" & LastRow).Offset(1, 0).Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 7, FALSE)"
'For column E
LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
PartNumber = "B" & LastRow + 1
.Range("E" & LastRow).Offset(1, 0).Formula = "=VLOOKUP(" & PartNumber & "," & myRange & ", 7, FALSE)"
End With 'ThisWorkbook.Sheets("Sheet1")

how to initialise my counter in vba excel

I have a problem with my vba project.
My workbook has 4 sheets (Draft, cky, coy and bey), in the sheet "draft i have all my data and i want to reorganise them. the columns "G" of the sheet "draft" contains the values (cky, coy and bey).
I want my macro to go through the colums and copy all the cells that have the same value and paste them in their corresponding sheet starting at the cell (A2), for exemple: i want the macro to copy all the data that have "cky" and paste it in the sheet "cky" starting at the cell A2 and so on/
Below you can see what i have done so far:
Sub MainPower()
Dim lmid As String
Dim srange, SelData, ExtBbFor As String
Dim lastrow As Long
Dim i, j, k As Integer
lastrow = ActiveSheet.Range("B30000").End(xlUp).Row
srange = "G1:G" & lastrow
SelData = "A1:G" & lastrow
For i = 1 To lastrow
If InStr(1, LCase(Range("E" & i)), "bb") <> 0 Then
Range("G" & i).Value = Mid(Range("E" & i), 4, 3)
ElseIf Left(Range("E" & i), 1) = "H" Then
Range("G" & i).Value = Mid(Range("E" & i), 7, 3)
Else
Range("G" & i).Value = Mid(Range("E" & i), 1, 3)
End If
Next i
'Sorting data
Range("A1").AutoFilter
Range(SelData).Sort key1:=Range(srange), order1:=xlAscending, Header:=xlYes
'Spreading to the appropriate sheets
j = 1
For i = 1 To lastrow
If Range("G" & i).Value = "CKY" Then
Sheets("CKY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "BEY" Then
Sheets("BEY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
ElseIf Range("G" & i).Value = "COY" Then
Sheets("COY").Range("A" & j & ":E" & j).Value = Sheets("Draft").Range("C" & i & ":G" & i).Value
End If
j = j + 1
Next i
End Sub
Thank you to help
best regards
Use this refactored code in the For Loop and it should work for better for you:
For i = 1 To lastrow
Select Case Sheets("Draft").Range("G" & i).Value
Case is = "CKY","COY","BEY"
Dim wsPaste as Worksheet
Set wsPaste = Sheets(Range("G"& i).Value)
Dim lRowPaste as Long
lRowPaste = wsPaste.Range("A" & .Rows.COunt).End(xlup).Offset(1).Row
wsPaste.Range("A" & lRowPaste & ":E" & lRowPaste).Value = _
Sheets("Draft").Range("C" & i & ":G" & i).Value
End Select
Next i

Sum IF VBA error '438' - object doesn't support this property or method

I dont know why it gives me '438' - object doesn't support this property or method error, in the "Sumif" Function line.
How can I do the sumif function in VBA?
Application.WorksheetFunction.SumIfs(Range("N2:N" & RSum), Range("C" & rw), Range("S2:S" & RSum))
And here's the whole code:
Sub Macro1()
Dim LR As Integer
Dim rw As Integer
Dim RSum As Integer
LR = Range("C" & Rows.Count).End(xlUp).Row
RSum = Range("n" & Rows.Count).End(xlUp).Row
For rw = 2 To LR
If Not IsEmpty(Range("C" & rw).Value) Then
Range("G" & rw).Value = Application.WorksheetFunction.SumIfs(Range("N2:N" & RSum), Range("C" & rw), Range("S2:S" & RSum))
End If
If Range("G" & rw).Value = 0 Then
Range("G" & rw).Value = "-"
Range("F" & rw).Value = "No"
ElseIf Range("G" & rw).Value <> 0 Then
Range("F" & rw).Value = "Yes"
End If
Next rw
End Sub
Excel Version 2002.
Maybe your second and third parameters are switched ?
2nd parameter sould be Range criteria_range1 , and 3rd parameter should be criteria1.
Try:
Range("G" & rw).Value = Application.WorksheetFunction.SumIfs(Range("N2:N" & RSum), Range("S2:S" & RSum), Range("C" & rw))
This seems correct just you need to understand the criteria of Sumifs formula.
the order of Sumifs are-
1.Sum_range
2.Criteria_range
3.Criteria and so on.
So the correct syntax would be-
Range("G" & rw).Value = Application.WorksheetFunction.SumIfs(Range("N2:N" & RSum), Range("S2:S" & RSum), Range("C" & rw))
For more such questions you can check-
https://knowledgebase.techandmate.com
Thanks,