Cross-worksheet cell comparison and update - excel

I'm trying to compare the value of one worksheet ("UserProf") column to a value in another worksheet ("DeptClasses") column. If those values are the same, update a separate cell in the first worksheet with a value from another column from the second worksheet. This will also need to loop through the values in the first worksheet looking for a match.
In essence, if the value in A1 in sheet 1 equals the value in sheet 2's cell D1, or D2, or D3, etc., set the value in cell A2 on sheet 1 to the value in sheet 2's matching row next column (i.e. if D1 in sheet 2 matches sheet 1 A1, set A2 to the value currently in sheet 2's E2 cell, etc.).
Each sheet's column has unique values (no duplicates), and is a number but stored as a text value. I tried changing the format of the cells to Number, with no change in the results. The range of columns in each sheet is fixed (hence setting the "To lastXxx" as a fixed integer and not using End(xlUp).Row to set the value. I've used this approach for other spreadsheets/cross-sheet comparison and it works as I expect.
When troubleshooting, I did add more variables to display in the Locals window in VBA and 'stepped into' the script - that is, see the values that were being evaluated, etc.; from what I can tell the "If" statement never evaluates to "True" even when I can see in the Locals that the cell values being compared do match.
There may be a more efficient way to set up the range, etc., but this was a way that worked for me in the past, so was trying to keep in that format - mostly because I understand it.
Sub Profiles()
Dim lastDepClass As Integer
Dim lastClass As Integer
Dim DepClass As Integer
Dim Class As Integer
lastDepClass = 137
lastClass = 106
For Class = 3 To lastClass
For DepClass = 2 To lastDepClass
If Sheets("UserProf").Cells(Class, 1).Value = Sheets("DeptClasses").Cells(DepClass, 5).Value Then
Sheets("DeptClasses").Cells(DepClass, 6) = Sheets("UserProf").Cells(DepClass, 5).Value
End If
Next DepClass
Next Class
End Sub

Sub Profiles()
Dim ws1 As Worksheet, ws2 As Worksheet 'Define worksheet variables
Dim x As Long, y As Long 'Define last row variables
'Assign your variables
Set ws1 = ThisWorkbook.Sheets("UserProf")
Set ws2 = ThisWorkbook.Sheets("DeptClasses")
For x = 3 To 106 'Just use the last row numbers, unless the lastrow is dynamic
For y = 2 To 137 'for each x row loop in ws1 loop through all y rows
'in ws2 until a match is found
If ws1.Cells(x, 1).Value = ws2.Cells(y, 5).Value Then
ws1.Cells(x, 6).Value = ws2.Cells(y, 5).Value
End If
Next y
Next x
End Sub

Related

Assigning cell value based to vlookup result - VBA

After failing to figure out how to do that for a while, I'll try my luck here:
I'm essentially trying to compare two situations using VBA.
A similar (and a lot simpler) example:
F2, for example, calculate 152+D2, while F3 calculates 185+D3.
I wish to run a macro that would check the effect of one person getting a different amount of points. For example, if A2 = Max the macro should assign the value of A3 (18) to D3. If A2 = Lewis, 18 would become the new value of D2.
Tried using vlookup and match+index in order to find the cell that I want to change. When using vlookup, the code looked similar to this:
First I copied F2:F4 to I2:I4, so the results would be comparable. Then tried to replace the value of D2:D4 according to A2&A3:
name = Range("A2").value
newvalue = Range("A3").value
Find = Application.VLookup(name, Range("C2:D4"), 2, False)
Find.value = newvalue
Perhaps I should be looking for the cell itself, and not the value, and then it would work (maybe using offset, or offset+match? couldn't make it work)?
Would appreciate any help!
Not really sure what the intention is but this seems like a fun challenge.
So logic is this. We look for the name in column C. If we get a match we will get a row back as an answer, then we replace the value from "A3" and add it to the row we got but to the column D.
Maybe something like this :D?
Option Explicit
Sub something_test()
Dim lookup_val As String
Dim lrow As Long
Dim lookup_rng As Range
Dim match_row As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1") 'Name the worksheet
lrow = ws.Cells(Rows.Count, "C").End(xlUp).Row 'Find last row in Sheet1
lookup_val = ws.Cells(2, "A").Value 'Set the lookup value
Set lookup_rng = ws.Range("C2:C" & lrow) 'set the lookup range
match_row = Application.Match(lookup_val, lookup_rng, 0) + 1 'Find the name in column C. Add +1 since the range starts at row 2. We will get the row number back
ws.Cells(match_row, "D").Value = ws.Cells(3, "A").Value 'Take the value from "A3" and replace the existing value at the row we found, but for column D
End Sub

My nested For-Each loop isn't going through each cell in a given range

In Excel VBA:
My second For-Each loop is only going through the first cell in a given column.
I need help getting it to go through each cell in the entire column.
In my code included here, I want to loop through each cell in a row (no issue). then once a cell in the row matches my criteria (no issue), I want to loop through each cell (celltwo) in that cell's column (issue).
I'm able to loop through the row and identify my criteria, but then the second for-each loop only considers the first celltwo in the given column. So I never get a celltwo with .row >=10, the first cell in each column has row=1.
Any help is appreciated.
This is for VBA in Excel. I've tried different ways of identifying my second range to loop through but nothing has allowed the second for-each loop to cycle back from "Next Celltwo" to the beginning of the loop.
Sub WriteSummary()
Dim UploadRange As Range
Dim SummaryRow As Integer
Dim CategoryRange As Range
Dim Cell As Range
Dim Celltwo As Range
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Set Variables
Set MacroFile = ThisWorkbook
Set MacroSheet = ThisWorkbook.Worksheets("Macro")
Set UploadDash = ThisWorkbook.Worksheets("Upload Dash")
Set SummarySheet = ThisWorkbook.Worksheets("Summary")
Set IndexSheet = ThisWorkbook.Worksheets("Indexes")
Set CategoryRange = UploadDash.Range("5:5")
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Determine Output Row
SummaryRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
''''''MY PROBLEM STARTS HERE'''''''
For Each Cell In CategoryRange
If Cell.Value = 8840 Then
For Each Celltwo In SummarySheet.Cells(, Cell.Column).EntireColumn
i = MsgBox(Celltwo.Row, vbOKOnly)
If Celltwo.Row >= 10 Then
If Celltwo.Value > 0 Then
o = MsgBox(Celltwo.AddressLocal)
SummaryRow = SummaryRow + 1
Else
End If
Else
End If
Next Celltwo '''''DOES NOT LOOP'''''
Else
End If
Next Cell
I expect that when the code finds cell.value = 8840 it will then loop through each cell in that cell's column. Instead, it only loops through the first cell in that column and exits the second for-each loop
Cells needs a row and column argument, but I think it's the EntireColumn which is taking the whole column as a single range.
Try something like this instead, which will restrict to cells containing something.
It starts at row 6 so amend to suit.
With SummarySheet
For Each Celltwo In .Range(.Cells(6, Cell.Column), .Cells(.Rows.Count, Cell.Column).end(xlup))
' etc
Next
End With

Getting a list of cells that contain substring using VBA

I am wondering how I can generate a list of cells in Excel file that contain a given substring using VBA. This should be able to find the cells regardless of the upper/lower case.
An example is:
Given the user-defined inputs (apple and berry), it sholud return the second picture.
How do I do this in VBA?
You say generate a list... So I assume you won't override your old data.
This code checks for the two values in worksheet "Sheet1". Then compares the two values you define against the cell value in your data (your data is assumed to be in Column A, from row 1 and downwards). If either of defined values exist in the cell (apple or berry, regardless of small/big letters), it's considered a match. If match is found it will copy the value to the first empty row in Column B.
VBA Code:
Sub SearchAndExtract()
Dim lrow As Long
Dim lrowNewList As Long
Dim i As Long
Dim lookupValue As String
Dim lookupValue2 As String
Dim currentValue As String
Dim MySheet As Worksheet
Set MySheet = ActiveWorkbook.Worksheets("Sheet1")
lookupValue = "*apple*" 'First name you want to search for. Use * for wildcard
lookupValue2 = "*berry*" 'Second name you want to search for. Use * for wildcard
lrow = MySheet.Cells(Rows.Count, "A").End(xlUp).Row 'Find last row in your data column
lrowNewList = MySheet.Cells(Rows.Count, "B").End(xlUp).Row 'Find last row in the column you want to paste to
For i = 1 To lrow 'From Row 1 to last row in the column where you want to check your data
currentValue = MySheet.Cells(i, "A").Value 'Define the string value you have in your current cell
If LCase$(currentValue) Like LCase$(lookupValue) Or _
LCase$(currentValue) Like LCase$(lookupValue2) Then 'LCase for case sensitivity, it check the current cell against the two lookup values. If either of those are find, then
MySheet.Cells(lrowNewList, "B") = MySheet.Cells(i, "A") 'Copy from current cell in column a to last blank cell in column B
lrowNewList = lrowNewList + 1
End If
Next i
End Sub

Working with the Columns function - Excel VBA

I have been looking around the site for a while for an answer to this question but no luck just yet. I have this code where I loop through a row of numbers and depending on what number is in the cell at the time, determines what I copy and paste to the sheet. I am using Columns for this because it is the only way I can make my code dynamic. It works but when I paste I would like to paste in cells lower than where it's pasting right now. I was wondering if Columns had a way of specifying what column and where to paste my data.
Code:
Dim sh As Worksheet
Dim rw As Range
Dim row As Range
Dim cell As Range
Dim RowCount As Integer
Set rw = Range("A5:CG5")
Set sh = ActiveSheet
For Each row In rw.Rows
For Each cell In row.Cells
Select Case cell.Value
Case "2"
ThisWorkbook.Worksheets("Sheet1").Range("E27:E51").Copy Destination:=Sheets("Sheet2").Columns(4)
End Select
Next cell
Next row
Your problem can be solved as Jeeped said, use Destination:=Sheets("Sheet2").Cells(27, 5) or Destination:=Worksheets(2).Range("E27")
Since you want to learn a little bit more, i made an example explanation:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-column-property-excel
On the link it is explained that .Column:
Column A returns 1, column B returns 2, and so on.
And the same is with the .Rows
Use .Cells https://msdn.microsoft.com/pt-br/library/office/ff194567.aspx So you can use the .Cells(Rows,Columns) or .Cells(Index from a Range) or the entire Object:
With Worksheets("Sheet1").Cells.Font
.Name = "Arial"
.Size = 8
End With
So an example if you want to turn your spreadsheet dynamical: to copy from range $E$27 to last row with something written from column $E on Sheet1 To
the last column with nothing written on row 1 on Sheet2.
Sub test()
'Declare variables here
Dim sht1, sht2 As Worksheet
'sht1 has the data and sht2 is the output Worksheet, you can change the names
last_row = Worksheets(1).Range("E65536").End(xlUp).Row
last_column = Worksheets(2).Cells(1, sht1.Columns.Count).End(xlToLeft).Column
'Data add
For i = 27 To last_row
'Start from Row 27
Worksheets(2).Cells(i - 26, last_column + 1) = Worksheets(1).Cells(i, 5)
Next i
MsgBox "Data Updated"
End Sub
And an example of a basic dynamical workbook with i=i+1 and For loops split a single row of data into multiple unique rows into a new sheet to include headers as values and cell contents as values

Excel VBA code to copy a row if an uppercase or a lowercase in a column to next sheet

I want to copy a row to another sheet (sheet4) when in column Z a upper case X is present and when on another row a lower case x is present in the same column. Thus automatically copying both rows to sheet4.
To further explain
I need the VBA code to pick up both letters throughout the current sheet in just column Z
Thanks
Something like this shoudl do it.
Sub CopyData()
Dim Rng As Range, cell As Range
Dim rw As Long
Set Rng = Worksheets("Sheet1").Range("Z:Z")
rw = 1
For Each cell In Rng
If LCase(cell.Value) = "X" Then
Worksheets("Sheet2").Cells(rw, "A") = cell.Offset(0, -1)
rw = rw + 1
End If
Next
End Sub
However, I don't know what this means:
"when on another row a lower case x is present in the same column."
What is the logic???

Resources