I'm new to VBA(coding) i just need your help on a work that i'm doing. the process is on Sheet1 we have the raw data as you can see on the attachment
we have the names on column a & orders and column b. i just want to copy the orders which is on column b basing on the name from column a from sheet1 to sheet2 sample image on the attachment on the result
Also if we have a name on sheet1 that is not available in sheet2 we could just ignore or skip the order unless we added the name on sheet2. also the other condition is if we could skip a cell if it has values already we can only paste it if cells from column b on sheet2 are blank. like if john's order has been change on sheet1 from dumplings to soup. i want John to have the same first order he took earlier which is dumplings that is saved on sheet2 earlier unless i deleted the first order. hope you guys can help appreciate your time a lot.
this is would be my current code all i need is the code to skip cells with values in sheet2 on column b
Sub Rectangle1_Click()
Dim I, total, fRow As Integer
Dim found As Range
total = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To total
answer1 = Worksheets(2).Range("A" & I).Value
Set found = Sheets(1).Columns("A:A").Find(what:=answer1)
If found Is Nothing Then
Worksheets(2).Range("A" & I).Value = ""
Else
fRow = Sheets(1).Columns("A:A").Find(what:=answer1).Row
Worksheets(2).Range("B" & I).Value = Worksheets(1).Range("B" & fRow).Value
End If
Next I
End Sub
Related
I am trying to build code to do the following:
In a workbook I have two sheets.
Sheet 2 has several columns, I want to create a loop to read each row in a particular column, lets say column D. When the row in that column is not equal to zero, I want to copy the value in that row to a column in Sheet2, Lets say its Column G in sheet 2 but it would be the same row from column D in sheet 1.
Example
Sheet 2 Sheet 1
Column D Column G
0 Don’t copy from Sheet 2
500 500 (Copied from Sheet 2)
20 20 (Copied from Sheet 2)
I was able to figure this out using an example from another post as well as looking at Felipe's suggestion.
I was using a function to find the last row in one of my sheets within a workbook as I was coping data. By using this lastrow function I created a loop and was able to use an IF statement that allowed me to copy the row that had data to a column(s) in my other sheet
FirstRow = Range("A2").Value
'Find the last non-blank cell in column A(1)
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim b As Integer, Buy As Integer
For b = 2 To LastRow
If Range("F" & b).Value <> 0 Then
Range("F" & b).Select
Selection.Copy
Sheets("T-1 BSR").Select
Range("I" & b).Select
ActiveSheet.Paste
Range("J" & b).Select
ActiveSheet.Paste
Sheets("BAML Futures PnL").Select
End If
Next b
I also used the same logic as I had to use another column and multiply it by -1 when there was a sell
Dim s As Integer, Sell As Integer
For s = 2 To LastRow
If Range("G" & s).Value <> 0 Then
Sell = Range("G" & s) * -1
Range("G" & s).Value = Sell
Range("G" & s).Select
Selection.Copy
Sheets("T-1 BSR").Select
Range("I" & s).Select
ActiveSheet.Paste
Range("J" & s).Select
ActiveSheet.Paste
Sheets("BAML Futures PnL").Select
End If
Next s
Not sure if this was the most efficient way but it does work
I would go to create dynamic ranges & declare them in my VBA code.
So, after creating a dynamic range (using the Name Manager to define dynamic ranges using OFFSET), which is explained in here: Create Dynamic Range (Offset), ...
... I would declare them at the beginning of my code. To declare a range using a the Name Manager, look here Excel Named Ranges.
... then name the column within the range, in order to assign a value to it. The following link leads to the Column property in Range.Columns.
... then, create your loop using Range_1 in Range_2 (Column): Loop through Defined Range.
... Finally, just compare the values using an If (If Statement), retrieving the position of the cell when it's successful (Range.Column & Range.Row) & copying it to the second sheet (Range.Value).
I have looked and attempted to fit whatever code I have run across on this site (since this morning) into a working application with non-functional or poor results.
I need to be able to match if id numbers in the "A" column of sheet 1 match in the respective "A" column of sheet 2, and just paste the matches to column "A" in sheet 3. The column range in sheet 1 is not the same range as sheet 2, and sheet 3 needs to have the matches pasted in each row as they are found (i.e. no blank rows in between). I do not need to do anything about numbers that do not match.
I have attempted to modify code from a different thread; it catches some matches, but not all. I guess the ranges are incorrect but I don't really know how to code that in VBA. Plus there are spaces in the column where some of the other duplicates should be posted. The "if found is nothing then x = 0" is unnecessary (and may even be causing the aforementioned error), but if I take that out I get a type mismatch error.
Sub matchPAs()
Dim x, i, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To total
match1 = Worksheets(1).Range("A" & i).Value
Set found = Sheets(2).Columns("A:A").Find(what:=match1)
If found Is Nothing Then
x = 0
Else
fRow = Sheets(2).Columns("A:A").Find(what:=match1).Row
Worksheets(3).Range("A" & i).Value = Worksheets(1).Range("A" & fRow).Value
End If
Next i
End Sub
I have an excel document that is created from another deployment tool that reads in array values and creates the excel document. I'm trying to add the ability to loop through column b and find null value and merge cell a thru c.
The logic I would like to use is loop through column B and look for empty cells, get the cell row number and use that to determine where the value i want to merge and center is (i.e. server1 in the picture below). I would then merge and center that row and continue through the loop until I reach the end of the used range.
I have tried using the Range.FindNext(string value) property but was unsuccessful in getting it to work.
Example of current layout is as follows:
What I am shooting for:
You could loop through every row in the used range of the sheet, looking for rows with empty values ("") in Column B, then merging Columns A to C of that row. If you want to run it from PowerShell, you can create a new module in Excel, then open the excel file from PowerShell, run your module from PowerShell, then save. Hope that helps.
Public Sub Test()
Dim i As Long
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'Only loop through used rows
'skip row 1 since it has the headers
For i = 2 To lastRow
If Range("B" & i) = "" Then
Range("A" & i & ":C" & i).Merge
Range("A" & i & ":C" & i).HorizontalAlignment = xlCenter
End If
Next
End Sub
Im creating a macro in excel that has search function. so basically. i will put the list of servers in Sheet3 Column A and it will search the servers ing Sheet1 column A. Once done, it will display the result in Sheet 2 column A. However, im having issue in searching for wildcard. for example. i want to seach server "ABC123" but it will not display the result because it needs to be the whole name of the server "ABC123.def" can anyone help me with the codes?
Option Explicit
Sub HostNameFinder()
Dim srchLen, hName, nxtRw As String
Dim g As Range
'Clear Sheet 2 and Copy Column Headings
Sheets(2).Cells.ClearContents
Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
'Determine length of Search Column from Sheet3
srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
'Loop through list in Sheet3, Column A. As each value is 'found in Sheet1, Column A, copy it top the next row in Sheet2
With Sheets(1).Columns("A")
For hName = 2 To srchLen
Set g = .Find(Sheets(3).Range("A" & hName), lookat:=xlWhole)
If Not g Is Nothing Then
nxtRw = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
End If
Next
End With
End Sub
If you fire up the macro recorder and perform a search for "ABC123" with the option to look in formulas and the option "Match entire cell contents" NOT selected, Excel will find the cell with the value ABC123.def
You could then study the code created by the macro recorder and would notice the difference to your code. You use lookat:=xlWhole, i.e. you're looking at the whole cell. Change it to LookAt:=xlPart. This is the equivalent of using a wildcard.
Or build the search term by wrapping it in a set of asterisks.
Set g = .Find("*" & Sheets(3).Range("A" & hName) & "*", lookat:=xlWhole)
I have two worksheets within the same workbook, namely sheet1 ("rawdata") and sheet2 ("Overview).
I copy downloaded data into sheet1 ("rawdata"). Here the number of rows vary but heading/columns are always the same. After this I need to copy specific cells into another worksheet.
Here are the "rules" I was thinking about:
1) Always copy cells from the rawdata sheet E9, W9, X9 and Y9 into a specific cell in the target sheet. I had something like this (which worked):
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E9").Value
2) Always copy the value within column E in the lastrow. However, the last row is varying from rawdata to rawdata while the column (E) stays the same. I tried something like this: (not working)
....= Worksheets("rawdata").Range("E1").End(xlDown).Value
3) The script should be linked to the button, when I click the button again to insert the data from the sheet rawdata, the data should be inserted in the next (following) column of worksheet overview.
Assumes column E always has data. Which in this case should be true.
Sorry tried to simplify and broke it.
LastRow_WithDataInColumnE = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row
Should be
With Worksheets("rawdata")
LastRow_WithDataInColumnE = .Range("E" & .Rows.Count).End(xlUp).Row
End With
Now .Rows.Count should refer to Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = Worksheets("rawdata").Range("E" & .Rows.Count).End(xlUp).Row.Value
Should be
With Worksheets("rawdata")
Worksheets("overview").Range("X10").Value = .Range("E" & .Rows.Count).End(xlUp).Row.Value
End With
There is a discussion here Error in finding last used cell in VBA. Suggests a better solution for situations where there is no data in Column E or where rows have been deleted.
You could do something like this to get the last data range in column E:
Public Function FindLastColumnECellAvailable()
FindLastColumnECellAvailable = "E" & WorksheetFunction.CountA(Range("E:E"))
End Function
Then you will have this:
At the end just read the cell value:
Range(FindLastColumnECellAvailable).Value
Greetings
Sorry
An apologize "in advance", I just read the date, hope this will help you yet or anyone else, it's my second day