currently I am working with an Excel report that has 135.000 rows. There are assets listed in it and I want to count each asset and write it into another worksheet.
I have tried to write a VBA script, which you can find below. It just copies one entry but does not iterate over each row of the worksheet.
Sub assetVulnerabilityCount()
With Sheets("tblExport")
assetCount = 1
rowMax = Sheets("tblExport").Cells(.Rows.Count, "F").End(xlUp).row
currentAsset = Sheets("tblExport").Range("B" & row).Value
For row = 2 To rowMax
If currentAsset = Sheets("tblExport").Range("B" & row).Value Then
Sheets("tblTarget").Range("B" & assetCount).Value = Sheets("tblTarget").Range("B" & assetCount).Value + 1
Sheets("tblTarget").Range("A" & assetCount).Value = currentAsset
Else:
currentAsset = Sheets("tblExport").Range("B" & row).Value
assetCount = assetCount + 1
End If
Next Zeile
End With
End Sub
Ideally, it would look like this:
Worksheet1:
Asset Names:
Laptop1337
Laptop1337
Laptop1337
PC420
PC420
Worksheet2:
Asset Name: Amount:
Laptop1337 3
PC420 2
Worksheet1 is what I have and Worksheet2 is what I need.
If we assume rowMax has the number of rows you need to iterate then
For row = 2 To rowMax
Should loop the rows if
Next Zeile
Is replaced with
Next row
Since it's row that is the variable in the loop.
Related
I am trying to copy and paste the entire entire columns values from sheet named Hey, to another columns in a sheet named final.
I dont want to use copy, paste or select option.However, my code is copying only the 1st line.
IntLastRow = Sheets("Hey").Cells(Cells.Rows.Count, "A").End(xlUp).Row
' IntRLastRow = Sheets("Interdiction Review").Cells(Cells.Rows.Count, "A").End(xlUp).Row
If Sheets("Hey").Range("H2") = "" Then
Sheets("Final").Range("A2").Value = Sheets("Hey").Range("G2:G" & IntLastRow).Value
ElseIf Sheets("Hey").Range("H2") <> "" Then
Sheets("Final").Range("A2").Value = Sheets("Hey").Range("H2:H" & IntLastRow).Value
EndIf
maybe do you know what I am doing wrong?
I need to copy dates from 2 or more columns to another column located in a different worksheet.
However, I am not sure if I can use 2 last row in the same line.
I am trying this code, but only paste the value of 1 line.
intrlastrow = Sheets("Final").Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1
Debug.Print intrlastrow
If Sheets("Hey").Range("AJ2") = "" Then
Sheets("Final").Range("A" & intrlastrow, "A" & IntLastRow).Value = Sheets("Hey").Range("AI2:AI" & IntLastRow).Value
ElseIf Sheets("Hey").Range("AJ2") <> "" Then
Sheets("Final").Range("A" & intrlastrow, "A" & IntLastRow).Value = Sheets("Hey").Range("AJ2:AJ" & IntLastRow).Value
End If
I need to add the values of 2 or more columns from sheet named HEY to the same column in the destination sheet (Final sheet)
Unless the range you are trying insert the values from is as large as the one you are taking them from, only the data which fits will be copied. Changing your code to something like
Option Explicit
Sub test()
Dim IntLastRow As Long
IntLastRow = Sheets("Hey").Cells(Cells.Rows.Count, "A").End(xlUp).Row
' IntRLastRow = Sheets("Interdiction Review").Cells(Cells.Rows.Count, "A").End(xlUp).Row
If Sheets("Hey").Range("H2") = "" Then
Sheets("Final").Range("A2:A" & IntLastRow).Value = Sheets("Hey").Range("G2:G" & IntLastRow).Value
ElseIf Sheets("Hey").Range("H2") <> "" Then
Sheets("Final").Range("A2:A" & IntLastRow).Value = Sheets("Hey").Range("H2:H" & IntLastRow).Value
End If
End Sub
should do the trick.
You probably notice that I've changed Sheets("Final").Range("A2") to Sheets("Final").Range("A2:A" & IntLastRow), similar to what you've done to decide what range to copy values from.
I have a sheet called "Table" where I have the table I'm looking up its A2:B20,
A2:A20 contains numbers in "XX" format these are the numbers I will be looking up.
The B2:B20 part of the table contains text is this text I want to use to replace values with.
I have my main sheet (currently called "Test") which contains my data, I want to look in Column M and check if I can find a value where the first 2 chars match any one of the values in A2:A20, if I do find a match I then want to replace the value of column F on my data sheet (Test) with the corresponding value from B2:B20 if not I want to leave it as is and move on.
I'm running into problems as the data in column M is numbers stored as text and it is replacing the wrong value when the table list 1 or 11 or 2 and 22.
'
Dim MyString As String
Counter = 2
1:
MyString = Sheets("Table").Range("A" & Counter).Value
For X = 1 To Range("M" & Rows.Count).End(xlUp).Row
If Replace(MyString, Left(Sheets("TEST").Range("M" & X).Value, 2), "") <> MyString Then Sheets("TEST").Range("F" & X).Value = Sheets("Table").Range("B" & Counter).Value
Next
Counter = Counter + 1
If Counter <= Range("M" & Rows.Count).End(xlUp).Row Then
GoTo 1:
Else
End If
End Sub
I solved my own problem, I was doing too much - simplified it forces values to .text and my issues went away.
Sub BBK_Name()
'Checks column U for start of data (1st 2 chars)
' if they match an entry in bank table changes entry in column G to match table entry.
'
Dim MyString As String
Counter = 2
1:
MyString = Sheets("Table").Range("A" & Counter).Text
RplcValue = Sheets("Table").Range("B" & Counter).Text
For X = 1 To Range("M" & Rows.Count).End(xlUp).Row
If Left(Sheets("TEST").Range("M" & X).Value, 2) = MyString Then _
Sheets("TEST").Range("F" & X).Value = RplcValue
Next
Counter = Counter + 1
If Counter <= Range("M" & Rows.Count).End(xlUp).Row Then
GoTo 1:
Else
End If
End Sub
Hello I'd like to know if I could somehow take single text word from a column and make them a single cell in a row. This is hard to describe but hopefully I can provide a screenshot.
So the "Contents" word should be cut out and everything after that should be in their own cells with the id tagged next to the cell. The wordcount in one column can differ from 1 to 100. Same with the ID count.
Thanks in advance!
The following code should achieve the goal you give:
Private Sub SortAndSplit()
Dim docId, docContent, lastRow, docArray, myLoop, myArrayLoop, lastRowList
Dim wSheet: Set wSheet = ThisWorkbook.Worksheets("Sheet1")
lastRow = wSheet.Cells(wSheet.Rows.Count, 2).End(xlUp).Row ' Get last row in column 2 (B)
For myLoop = 2 To lastRow
docId = wSheet.Range("A" & myLoop).Value
docContent = wSheet.Range("B" & myLoop).Value
docArray = Split(Trim(Replace(docContent, "Contents ", "")), ",")
For myArrayLoop = 0 To UBound(docArray)
lastRowList = wSheet.Cells(wSheet.Rows.Count, 4).End(xlUp).Row + 1 ' Get last row in column 4 (D) and add 1
wSheet.Range("D" & lastRowList).Value = docId
wSheet.Range("E" & lastRowList).Value = docArray(myArrayLoop)
Next
Next
End Sub
I have an Excel file which has a key identifier in one row and the associated data in another row.
I need to combine the data in order to create meaningful reports.
Below is an example of the data and what I'd ultimately like it to look like in the end.
Current Data Format:
Row 1: Bob Smith
Row 2: Data about Bob that is not needed
Row 3: Bob's Totals (e.g. Data about Bob that is needed)
Row 4: -blank
Row 5: Jane Doe
Row 6: Data about Jane that is not needed
Row 7: Data about Jane that is not needed
Row 8: Jane's Totals (e.g. needed data)
And this is how I need the end product to look something like this with the name and totals row only on each line:
Row 1: Bob Smith & Bob's Totals
Row 2: Jane Doe & Jane's Totals
Lets assume "Bob Smith" is in A1 and "Bob's Totals" is in A3.
If you need the combined data in and one cell (Lets say A10) then
Private Sub abc()
Worksheets("Nameofyoursheet").Range("A10").Value = Worksheets("Nameofyoursheet").Range("A1").Value & " " & Worksheets("Nameofyoursheet").Range("A3").Value
End Sub
Similarly you can go on to do the same for "Jane" & "Jane's Totals"
You can modify the & " " & part of the code to include any text you like to come in between, for instance & " the totals are" &
Hope this is what you were looking for.
My approach is pretty rudimentary and straightforward. The logic is as follows:
Copy the sheet to a new sheet called Dummy to be safe.
Delete all unwanted rows. We leave everything with Agent or Totals (which was why I was asking for keywords).
Iterate over the new rows and just copy and paste the total rows to the cell beside the name.
Set-up:
After first two steps:
After whole code is finished:
Code is as follows:
Sub WeirdConsolidate()
Dim SourceSh As Worksheet, DummySh As Worksheet
Dim DummyLastRow As Long, NewLastRow As Long
Dim Iter As Long, Iter2 As Long
With ThisWorkbook
Set SourceSh = .Sheets("Sheet1")
SourceSh.Copy After:=.Sheets(Sheets.Count)
Set DummySh = ActiveSheet
End With
With DummySh
Application.ScreenUpdating = False
.Name = "Dummy"
DummyLastRow = .Range("A" & Rows.Count).End(xlUp).Row
For Iter = DummyLastRow To 1 Step -1
If InStr(1, .Range("A" & Iter).Value, "Totals") = 0 And _
InStr(1, .Range("A" & Iter).Value, "Agent") = 0 Then
.Rows(Iter).EntireRow.Delete
End If
Next Iter
NewLastRow = .Range("A" & Rows.Count).End(xlUp).Row
For Iter2 = 2 To NewLastRow
.Range("B" & Iter2 & ":D" & Iter2).Copy .Range("B" & (Iter2 - 1))
.Rows(Iter2).EntireRow.Delete
Next Iter2
Application.ScreenUpdating = True
End With
End Sub
Modify parameters accordingly. Let us know if this helps.
I have one excel Sheet having below records
id Empname state
1 steve NJ
2 karl NYC
I have to write one macro to prepare sql update stement like below and insert into new sheet within the same workbook.
UPDATE emp SET state='NJ' WHERE id=1
UPDATE emp SET state='NYC' WHERE id=2
Any suggestions or ideas please.
Regards,
Raju
Use below sub and you should be all set
Sub generateUpdate()
Dim myRow As Integer: myRow = 2 'Starting Row of data in source table
Dim temp As Integer: temp = 1
Do Until Sheet1.Cells(myRow, 1) = "" 'Loop until you find a blank
'Do Until myRow = 5 '5 is Row number till while you wish to loop
Sheet2.Cells(temp, 1) = "UPDATE emp SET state='" & Sheet1.Cells(myRow, 3) & "' where id = " & Sheet1.Cells(myRow, 1)
myRow = myRow + 1
temp = temp + 1
Loop
End Sub
You could do this with formulas; there's no need to write a macro (if I read your requirements correctly).
If your data is in Sheet1!A2:C3, then on Sheet2 you could start in cell A1 with the formula:
="UPDATE emp SET state = '" & Sheet1!C2 & "' WHERE id = " & Sheet1!A2
And then extend the formula down the column to repeat the pattern. After that you can simply copy the cells and paste the query into wherever you're going to use it.