VBA Macro Search Difference in Range - vba

I need to check the range from A1 to AY1, B1 to BY1.... from sheet 1 and compare them with the range from A1 to AY1, B1 to BY1.... from sheet 2 and highlight in yellow the difference in sheet 1.
Sub copyWorkingFileToConsolidated()
Dim i As Integer
i = 5
Do While Sheet2.Range("A" & i).Value <> ""
Sheet3.Range("A" & i & ":D" & i).Value = Sheet2.Range("A" & i & ":D" & i).Value
Sheet3.Range("F" & i & ":Y" & i).Value = Sheet2.Range("E" & i & ":X" & i).Value
Sheet3.Range("A" & i & ":Y" & i).Interior.ColorIndex = 0
i = i + 1
Loop
getResourceLevel
End Sub
Please help
Thanks

Related

Double concatenation in different TextBox depending on category

I don't know if somebody already asked a question about it or not, i'm new to VBA but i've already done some C# programming.
Code
in Text :
For i = 1 To 5 'Category
For j = 1 To 7 'Each entry of the category
If UserForm1.Controls("Cat" & j & "Entry" & i).Value <> "" Then
Range("A" & i).Value = UserForm1.Controls("Cat" & i & "Entry" & j).Value
End If
Next j
Next i
So this is basicaly what i'm trying to do, i have 2 categories, each one have 7 TextBox, depending on how many of them are filled, I wanted to put their values on a cell... But seems like the concatenation isn't working, also tried the For Each methods but no results..
The interface
Thank you guys
You aren't concatenating anything you are overwriting cells A1-5 every time.
For i = 1 To 5 'Category
For j = 1 To 7 'Each entry of the category
If UserForm1.Controls("Cat" & j & "Entry" & i).Value <> "" Then
if range("A" & i).value <> "" then
Range("A" & i).Value = Range("A" & i).Value & " " & UserForm1.Controls("Cat" & i & "Entry" & j).Value
else 'Avoiding the space
range("A" & i).value = Userform1.controls("Cat" & i & "Entry" & j).value
end if
End If
Next j
Next i

Type mismatch (Error 13) in Loop

Line 4 is messing my loop up with a type mismatch! What am I doing wrong?
For i = 4 To 8
j = 20 + i
Col = Columns(j)
Range("'" & Col & "3'").FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
Try this:
For i = 4 To 8
Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
By using Cells or Range on their own Excel will assume you want to reference the active worksheet, in the active workbook. It's a much better idea to specify exactly which workbook/ worksheet you want the code to run on. E.g.:
For i = 4 To 8
ThisWorkbook.Worksheets("Sheet1").Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
You're doing many errors.
First, col is a column, not a (range address) string. You cannot concatenate a column to a string.
Second, you should not enclose a range address with single-quotes (').
What you probably wanted to do is:
Cells(3, j).Formula = ...

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

VBA to loop through set rows

I have the code below that updates data on the "Audit Sheet" with specific data from the "Master" sheet, prints the "Audit" sheet and loops until the last row is empty. It works great for a small amount of data, but I have another project that will have over 1800 rows of data. I don't want to clog up the printer with 1800 pages all at once.
What I want is to be able to have a box pop up and specify the beginning row and ending row. I have done this before, but I have forgotten over the years of how I originally wrote the code. Any help is appreciated.
Sub testLoopPaste()
Dim i As Long
Dim LastRow As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Master")
Set sht2 = wb.Sheets("Audit Sheet")
Application.ScreenUpdating = False
'Find the last row (in column A) with data.
LastRow = sht1.Range("A:A").Find("*", searchdirection:=xlPrevious).Row
'This is the beginning of the loop
For i = 2 To LastRow
'First activity
sht2.Range("B1" & ii) = sht1.Range("B" & i).Value
sht2.Range("B2" & ii) = sht1.Range("A" & i).Value
sht2.Range("B3" & ii) = sht1.Range("N" & i).Value
sht2.Range("H1" & ii) = sht1.Range("C" & i).Value
sht2.Range("H2" & ii) = sht1.Range("I" & i).Value
sht2.Range("H3" & ii) = sht1.Range("F" & i).Value
sht2.Range("K1" & ii) = sht1.Range("D" & i).Value
sht2.PrintOut
Next i
Application.ScreenUpdating = True
End Sub
You want to loop over a range object in a manner similar to
dim rngobj, userinputstart, userinputend as variant
set rngobj = Range(Range(userinputstart),Range(userinputend))
For each therow in rngobj
'do stuff here
Next
Depending on how you grab user input you're going to have to fiddle with that part.
Thank you to all who posted. I finally figured out what worked best. Here is my finished code and it works perfectly.
Sub testLoopPaste()
Dim i As Long
Dim LastRow As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Master")
Set sht2 = wb.Sheets("Audit Sheet")
Application.ScreenUpdating = False
'Find the last row of data
LastRow = InputBox("Enter the last row of data", "End Row")
'This is the beginning of the loop
For i = InputBox("Enter the first row of data", "Start Row") To LastRow
'First activity
sht2.Range("B2" & ii) = sht1.Range("B" & i).Value
sht2.Range("B4" & ii) = sht1.Range("C" & i).Value
sht2.Range("B6" & ii) = sht1.Range("D" & i).Value
sht2.Range("B8" & ii) = sht1.Range("L" & i).Value
sht2.Range("B10" & ii) = sht1.Range("M" & i).Value
sht2.Range("B12" & ii) = sht1.Range("N" & i).Value
sht2.Range("B14" & ii) = sht1.Range("Q" & i).Value
sht2.Range("B16" & ii) = sht1.Range("R" & i).Value
sht2.Range("B18" & ii) = sht1.Range("AO" & i).Value
sht2.Range("D2" & ii) = sht1.Range("J" & i).Value
sht2.Range("D4" & ii) = sht1.Range("K" & i).Value
sht2.Range("D6" & ii) = sht1.Range("O" & i).Value
sht2.Range("D8" & ii) = sht1.Range("A" & i).Value
sht2.Range("D10" & ii) = sht1.Range("AO" & i).Value
sht2.Range("D12" & ii) = sht1.Range("T" & i).Value
sht2.Range("D14" & ii) = sht1.Range("U" & i).Value
sht2.Range("D16" & ii) = sht1.Range("V" & i).Value
sht2.Range("D18" & ii) = sht1.Range("W" & i).Value
sht2.Range("D20" & ii) = sht1.Range("X" & i).Value
sht2.Range("D22" & ii) = sht1.Range("Y" & i).Value
sht2.Range("D24" & ii) = sht1.Range("Z" & i).Value
sht2.Range("D26" & ii) = sht1.Range("AA" & i).Value
sht2.Range("D28" & ii) = sht1.Range("AB" & i).Value
sht2.Range("D35" & ii) = sht1.Range("AT" & i).Value
sht2.Range("D37" & ii) = sht1.Range("AV" & i).Value
sht2.Range("D39" & ii) = sht1.Range("AX" & i).Value
sht2.Range("D41" & ii) = sht1.Range("AZ" & i).Value
sht2.Range("D43" & ii) = sht1.Range("BB" & i).Value
sht2.Range("D45" & ii) = sht1.Range("BD" & i).Value
sht2.Range("D47" & ii) = sht1.Range("BF" & i).Value
sht2.Range("D49" & ii) = sht1.Range("BH" & i).Value
sht2.Range("D51" & ii) = sht1.Range("BJ" & i).Value
sht2.Range("D53" & ii) = sht1.Range("BL" & i).Value
sht2.Range("D55" & ii) = sht1.Range("BN" & i).Value
sht2.Range("I2" & ii) = sht1.Range("F" & i).Value
sht2.Range("I4" & ii) = sht1.Range("G" & i).Value
sht2.Range("I6" & ii) = sht1.Range("S" & i).Value
sht2.Range("I8" & ii) = sht1.Range("AM" & i).Value
sht2.Range("I10" & ii) = sht1.Range("AN" & i).Value
sht2.Range("H12" & ii) = sht1.Range("AD" & i).Value
sht2.Range("H14" & ii) = sht1.Range("AE" & i).Value
sht2.Range("H16" & ii) = sht1.Range("AF" & i).Value
sht2.Range("H18" & ii) = sht1.Range("AG" & i).Value
sht2.Range("H20" & ii) = sht1.Range("AH" & i).Value
sht2.Range("H22" & ii) = sht1.Range("AQ" & i).Value
sht2.Range("H24" & ii) = sht1.Range("AI" & i).Value
sht2.Range("H26" & ii) = sht1.Range("AJ" & i).Value
sht2.Range("H28" & ii) = sht1.Range("AK" & i).Value
sht2.Range("H30" & ii) = sht1.Range("AL" & i).Value
sht2.Range("H35" & ii) = sht1.Range("AU" & i).Value
sht2.Range("H37" & ii) = sht1.Range("AW" & i).Value
sht2.Range("H39" & ii) = sht1.Range("AY" & i).Value
sht2.Range("H41" & ii) = sht1.Range("BA" & i).Value
sht2.Range("H43" & ii) = sht1.Range("BC" & i).Value
sht2.Range("H45" & ii) = sht1.Range("BE" & i).Value
sht2.Range("H47" & ii) = sht1.Range("BG" & i).Value
sht2.Range("H49" & ii) = sht1.Range("BI" & i).Value
sht2.Range("H51" & ii) = sht1.Range("BK" & i).Value
sht2.Range("H53" & ii) = sht1.Range("BM" & i).Value
sht2.Range("H55" & ii) = sht1.Range("BO" & i).Value
sht2.PrintOut
Next i
Application.ScreenUpdating = True
End Sub

Incrementing my loops correctly

The code below calculate an average between col E, colG and col I (like in the picture in column K)but something doesn't work how I need. My code doesn't take correctly column G. It takes in the loop 5,7,3,15,10 and not 5,5,7,3,3,3,15,15,10,10. It doesn't consider how many repetitive names are in column A. How to solve the problem? Anyone is able to help? thanks!
(look at the picture)
For f= 1 To ws.Range("F" & Rows.Count).End(xlUp).Row
nameF = ws.Range("F" & f).Value
For totRng = 1 To lastrowA
'if names from col A and col F coincide, then sum their numbers from col E
If nameF = ws.Range("A" & totRng).Value Then ws.Range("G" & f).Value =
ws.Range("G" & f).Value + ws.Range("E" & totRng).Value
On Error Resume Next
If nameF = ws.Range("A" & totRng).Value Then _
ws.Range("H" & f).Value = ((ws.Range("E" & f).Value / ws.Range("G" & f).Value)) * ws.Range("I" & f).Value
Next totRng
Next f
I should change this value: '/ ws.Range("G" & f).Value)'
Here a working piece of code
For f = 2 To ws.Range("F" & Rows.Count).End(xlUp).Row
nameF = ws.Range("F" & f).Value
For totRng = 2 To lastrowA
'if names from col A and col F coincide, then sum their numbers from col E
If nameF = ws.Range("A" & totRng).Value Then
ws.Range("G" & f).Value = ws.Range("G" & f).Value + ws.Range("E" & totRng).Value
End If
Next totRng
For totRng = 2 To lastrowA
'calculate the average
If nameF = ws.Range("A" & totRng).Value Then
Debug.Print nameF & " found on line " & totRng & " person we have is on line " & f & ". Formula is : ((" & ws.Range("E" & totRng).Value & "/" & ws.Range("G" & f).Value & ")*" & ws.Range("I" & totRng).Value & "=" & (((ws.Range("E" & totRng).Value / ws.Range("G" & f).Value)) * ws.Range("I" & totRng).Value)
ws.Range("H" & f).Value = ws.Range("H" & f).Value + (((ws.Range("E" & totRng).Value / ws.Range("G" & f).Value)) * ws.Range("I" & totRng).Value)
End If
Next totRng
Next f