Excel VBA Macro - vba

The List:- “Problem Sheet”(worksheet).
I have an excel sheet which represents a list of problem issues (Rows) which varies in size every day (eg more or fewer rows);
Each row has been assigned a name;
The assigned name is always in the same column “M” of the “Problem Sheet”;
An individual assignment name does not necessarily appear every day, or it may occur several times (on more than one row) on a given day;
I already have a macro that creates a Unique List (worksheet) of assignment names where each name appearing in column M of the Problem Sheet is recorded once in the “Unique List” worksheet;
The same macro creates a single new worksheet (in the same workbook) for every unique occurrence of an Assignment Name.
The Assignment Name is recorded automatically in the new individual worksheet tab.
Required:-
A macro that will check Column M of the main “Problem Sheet”;
For every row / problem where a particular assignment name occurs in Column M of the Problem Sheet, Match the assignment name with the worksheet of the same name, then Copy and Paste the details of the entire row from the “Problem Sheet” to the first blank row of the correct (same assigned name) worksheet in the existing workbook.
This routine must be repeated for every row in the Problem Sheet.

If order doesn't matter, this might be your best bet
Sub x()
Dim rngProbs As Range
With ThisWorkbook.Worksheets("Problem Sheet")
Set rngProbs = .Range("M1", .Range("M1").End(xlDown))
End With
Dim r As Range
For Each r In rngProbs
r.EntireRow.Copy
ThisWorkbook.Worksheets(r.Text).Rows(1).EntireRow.Insert
Next r
End Sub

Related

Need to copy/paste row (x number of times) when cell value equals X

I'm trying to use Excel for a Jewelry Order Form.
In the order form (sheet1), a user may select from a cell that is formatted into a drop-down list, a number representing the number of stones in a piece of jewelry. For example, if there are 10 stones in a ring, then the user selects 10 from the drop-down list.
The details for each of the 10 stones needs to be captured in the order form (Sheet1). For example, each stone will have 4 data elements... a stone type, weight, color, cut... So I created the desired formatted row of data (in Sheet2) where each cell is a drop-down for a user to select from.
I want to create a control button to do the following actions:
Delete rows 19:150 in Sheet1
This will clear out any prior stone details that may be displayed.
Find the value in cell C13 in Sheet1
This value will be used to determine how many rows should be pasted/displayed
Copy row, range A2: D2 in Sheet2
This is template row data where each cell in the row is its own drop-down list.
Paste row in B19 in Sheet1
This is the template row pasted into an order form.
4a) Paste as many rows as the value in step (2) above.
For example, if the value in step 2 from above is "3", then the stone details row will need to be pasted 3 times in the order form.
The furthest I've been able to get is the creation of the control button, and the delete clause...
Private Sub CommandButton1_Click()
Sub deleteMultipleRows()
Rows("19:150").Delete
End Sub
For the delete statement, you should probably use Sheet1.Rows("19:150").Delete as that will ensure that excel knows which sheet to delete those rows from.
You can declare a variable and assign it a value like so:
Dim rowCount as Integer
rowCount = Sheet1.Range("C13")
If you try recording a macro for the copy and pasting, you should see some sample code.
Note:
If the result looks something like this:
Sheet1.Activate
Row(2).Select
Selection.Copy
You can combine such statements like so:
Sheet1.Row(2).Copy
Since most .Activate commands can be ignored, and .Select and Selection. commands can be combined (and should be in most situations). The Sheet1. added before the .Row(2) tells Excel to specifically use Row 2 from Sheet1. Without the qualifier, i.e. just Row(2), Excel would use Row 2 from whichever sheet happens to be currently active.
You can then use a variable and the value from the sheet to loop through either pasting or copy/paste combo like so:
Dim counter as integer
For counter = 1 to Sheet1.Range("C13")
'Add code for copy/paste here
Next counter
Or if you have the rowCount variable declared and assigned, you could use it here like this:
Dim counter as integer
For counter = 1 to rowCount
'Add code for copy/paste here
Next counter

copy selected row cell entries to other sheet

I'm trying to provide the capability to select either a row or column in a row and copy certain cells off the row into a different sheet.
For example...I have a list of renters (rows) each with the amount they owe and the amount collected on one sheet and the other sheet is a receipt. I'd like to enter the rent collected and have the receipt sheet updated automatically with the other sheets rows/individuals name and amount collected.
Is there a VB function to identify the active row?
If I can get that working the next thing would be adding/incrementing the invoice number.
You can use a combination of UsedRange and the Rows() property to get your active row.
While UsedRange is not necessarily required, it would only select the used cells in that row as opposed to selecting the entire row of the workbook, which is very likely unneeded.
You can use this function:
Function rngActiveRow() As Range
Dim ws As Worksheet
Set ws = ActiveCell.Parent
Set rngActiveRow = ws.UsedRange.Rows(ActiveCell.Row)
End Function
If you wanted to test it, you can try calling this function in a test sub to see it work:
Sub test()
rngActiveRow.Select
End Sub

Variable Named Ranges in Excel

I have a table in Excel formatted as follows:
Date Asset Return
1/3/2005 0.003582399
1/4/2005 -0.01908258
1/5/2005 0.002080625
1/6/2005 0.005699497
1/7/2005 -0.008040505
1/10/2005 -0.00339116
1/11/2005 -0.009715187
1/12/2005 0.002371855
1/13/2005 -0.00580783
1/14/2005 0.001058481
1/18/2005 0.015483842
1/19/2005 -0.014690715
1/20/2005 -0.015714799
1/21/2005 -0.010796326
I need a named range to reference each column. The workbook is a template, so the named range won't always cover the same number of rows depending on the data. I want to set it so that the named range "Date" and the named range "Asset Return" are automatically sized to cover the entire column from the first value until the last, without going past the last value in the column.
It will always start at cell B8, but might end at a different row depending on the size of the data.
How can I set a dynamic named range to accomplish this?
This named range formula will do it:
=Sheet1!$B$8:INDEX(Sheet1!$B:$B,COUNTA(Sheet1!$B:$B)+8)
Remember to add the sheet name as the named range will operate on the active sheet otherwise.
The formula starts takes B8 as it's starting point: Sheet1!$B$8
It then counts how many cells are not blank in column B: COUNTA(Sheet1!$B:$B)
It adds 8 to the count (assuming your first rows are blank).
It then uses INDEX and the COUNTA to reference the last cell.
https://support.office.com/en-gb/article/INDEX-function-a5dcf0dd-996d-40a4-a822-b56b061328bd
https://support.office.com/en-gb/article/COUNTA-function-7dc98875-d5c1-46f1-9a82-53f3219e2509
Try this VBA code
Sub test()
application.DisplayAlerts = false
Range("B8").currentregion.createnames _
top:true, right:=false, left:=false, bottom:=false
application.DisplayAlerts = true
end sub

Copy row from one sheet and insert copied row under last row in another sheet

I am in need of your expert assistance.
I am trying to write some code that will copy rows and insert the copied row below the last row in another sheet.
I have a Global sheet that has the data i will be copying. It will need to look in column Q.
I think the problem will be when trying to copy the data, the data in column G is the text name of a Contract Code. But the sheets are name with the Number version.
for example i have a row that has BRREPAIRS in column Q, I need this to copy to Sheet 2870, then i have a row that has BRVOIDS in column Q, I need this to copy to Sheet 2781.
I could have multiple different Contract names so i think i might need to define the text to equal a sheet. So maybe Set BRVOIDS = Sheet.name("2781") Set BRREPAIRS = Sheet.name("2780") and so on until all sheets are defined.
When the data gets copied i need it to find the last row in column a that has data, when it is found it will insert the copied row into the sheet. for example EntireRow.Insert Shift:=xlDown.
I dont have any code at the moment. I would really appreciate all the assistance.
You don't need to do things like Set BRVOIDS = Sheet.name("2781"). In fact, that would be positively harmful since then you would need to run the data in Column Q through a possibly large Select statement to know what variable to use. Instead, you could write a function like
Function TargetSheet(ContractName As String) As Worksheet
'code which uses your secret list to determine target sheet
'Maybe a Select statement, Maybe a Vlookup -- who knows?
Set TargetSheet = 'sheet your code determined
End Function
Sounds like your code will be scanning down column Q, determining where to copy the corresponding row to. Once you get the above function working, you could combine it with something like this:
Function LastRow(TargetCol As Variant, Optional ws As Variant) As Range
'assumes TargetCol is something like 1 or "A"
Dim n As Long
If IsMissing(ws) Then Set ws = ActiveSheet
n = ws.Cells(1, TargetCol).EntireColumn.Rows.Count
Set LastRow = ws.Cells(n, TargetCol).End(xlUp).EntireRow
End Function
This returns as a range the last row containing data (or row 1 if the column is empty) in a specified column in a specified worksheet (which defaults to the Active sheet).
You haven't given enough to go on, but something along the lines of
LastRow("A",TargetSheet(Range("Q" & i).Value)).Insert Shift := xlDown
Might be what you are looking for. Why don't you try to work it out and ask another question (if need be) once you have some actual code?

excel 2010 - listobject table and worksheet range return different values - bug?

I have a table (table of listobject type). I sorted it, filtered it and viewed the sheet. All is well. Now I attempt to enumerate...
Where
oWs_ma is the worksheet that contains the table
oLO_maTable is the table on that worksheet
oRg are range objects
all vars are strings
In the loop below, the temporary range object returns the correct row number. This row number is the number the sheet shows. For example, the 1st data row in the table lies on sheet row 5. In all but one single row, out of many, worksheet and table objects return identical values. In the single instance where they do not, the worksheet is correct. Just one error in 30 or 40 rows. This is not an end point error. It happens midway in the table. There appears to be nothing unique before the error. In fact, the row value reported by oRg_tmp.row changes and points to the correct row!
Is this construct correct? There is additional code include for testing purposes, just to show that what I am getting is indeed "right"
For Each oRg_tmp In oLO_maTable.DataBodyRange.Rows.SpecialCells(xlCellTypeVisible).Rows
Set oRg_maOwer = oLO_maTable.Range.Cells(oRg_tmp.row, colndx_ma_it_owner)
Set oRg_maGLDept = oLO_maTable.Range.Cells(oRg_tmp.row, colndx_ma_gl_dept)
ma_owner = oRg_maOwer.Value2
ma_gl_dept = oRg_maGLDept.Value2
ma_owner_ws = oWs_ma.Cells(oRg_tmp.row, colndx_ma_it_owner).Value2
ma_gl_dept_ws = oWs_ma.Cells(oRg_tmp.row, colndx_ma_gl_dept).Value2
...
Next
If the construct is correct, this is solved. I will use the worksheet.
Thanks.