I'm reusing this string from another thread to copy a whole column WITH the blanks (needed for alignment of other information) to a new location. BUT I see that its copy action will stop at the first blank AND infact it does. What I need it to do is copy the blanks and everything as a block then put it under the Range as below. I considered filling all the blanks first but that just sends the fill value all the way to infinity. There will be more blanks than data.
Range(Range("P2"), Range("P2").End(xlDown)).Copy '!!!Stops at frist blank!!!
For idx = 1 To 1
Columns("P:P").Cut
Cells(Range("D2").End(xlDown).Row + 1, "D").Select
ActiveSheet.Paste
Next
I'm not seeing why it can't do that. I don't need it to be THIS code if there is some other solution. The task is that I'm changing the layout from one associated information fills a complete row to a "stacked" layout, where associated data (with some blanks) repeats down the column. So the segments are to be stacked. Cut copy pate with the whole columns has been mostly working.
It could copy a range based on the non empty value of another cell prior to moving it. BUT it needs to land on the first empty cell at the bottom of the new range. I'll repeat this for several columns but can do it separately.
I could bypass the first issue if I with some code that would look at the cells in columns BI through BO and fill them with a value ("0" or "-") IF the value in BH is NOT blank.
Your code and question is a little bit confusing but I think this is what you're looking for. this should copy all data from column P including blanks.
Range(Range("P2"), Range("P" & ActiveSheet.Cells(ActiveSheet.Rows.Count, "P").End(xlUp).Row)).Copy
Related
I've been trying soo hard much to clean up this csv data for a coworker.
I’m going to walk through what the data usually looks like and then walk through the steps I’ve done and then bring up what I’m currently struggling with… Bear with me as this is my first post (and I have no background in vba and everything is self-taught by Google).
So the data export is a csv which can be opened in excel broken out by several columns. The column in question is column G, which essentially has multiple data sets (1 – 219) for the same menu item (row).
For example:
A B C D E F G
Chicken Soup {1;$6.00;59;$9.00;88;$6.00}
Beef Soup {1;$8.00;59;$12.00;88;$8.00}
Duck Soup {1;$6.00;59;$6.00;88;$6.00}
Egg Soup {1;$8.00;59;$9.00;88;$8.00}
Water {1;$0.00}
French Onion Soup {1;$16.00;59;$15.00;88;$12.00}
Chili Soup {1;$17.00;84;$17.00}
So in column G, you can tell, there is multiple prices the format is:
{Column Number ; $ Price ; Column number $ Price etc & }
Regex: .[0-9]{1,2},[$][0-9]{1,3}[.][0-9][0-9].|[0-9]{1,2},[$][0-9]{1,3}[.][0-9][0-9]
The first goal was to parse out the data in the column into the row, in a format that is true to the csv (so it can be combined and resubmitted).
For example: (imagine there is a semi colon between each data set, as there should be in the final result)
{1;$21.00}
{1;$16.00}
{1;$12.00 5;$12.00 8;$12.00}
{1;$18.00 6;$18.00 8;$18.00}
{1;$10.00 6;$7.00 9;$12.00 11;$10.00}
{1;$20.00 6;$20.00 8;$20.00}
{1;$5.49 3;$3.99 10;$4.99 12;$4.99}
{1;$18.99}
{1;$21.00}
{1;$21.00}
To accomplish this goal, I wrote a macro that:
Copies column G from “Sheet1” and inputs to new sheet “Sheet2” in A1
Replace all “;$” with “,$” to help separate each data set by itself instead of having it broken out column name then dollar sign in two different columns
Text to columns macro splitting on “;” (and inputs results starting B1 so I can keep A1 with all the data sets in one column in case I need it) – also if you know how to keep the semi colon here, that would be helpful so I don’t have to re-add it in the future
Replace All from b1 to end of data set "," to ";" <-- to bring it back to original formatting
Copies the Data from B1 to last cell with data (data is not in order, the 50th row could have 219 columns and then the last row could only have 150) and pastes this data into column G of “rp items” (therefore overriding the existing data and shifting the columns as far right as the last column used.
However, when I showed my coworker what I’ve done, he wanted the leading number (column number) to correspond to the Columns (since data starts in column G, this will be column 1, H will be 2 etc). Therefore looking something like this so he can filter the column by the all the items that have that column number:
For example, this photo is how the outcome should look
So, now the goal is to create a macro that…
Loops through B1:B in sheet “STEP ONE” (column B starting at B1 then C1 then when blank in that row go to next row)
While (B1 (or next row) is blank, do nothing, end macro)
If B1 (or active cell) is not blank, read the cell value to extract column; copy the cell’s contents, paste in “STEP TWO” sheet in the same row as the active cell, but offset by the column number from cell value.
Toggle back to main sheet, goes to next cell within that row – if blank, go to next row and repeat until all data is done.
To give you some background, I have more than 25,000 lines of data (menu items) and the longest column (I believe is 219). So far, I’ve mostly been trying pieces of scripts I’ve found online but none of them are doing similar to what I need and I don’t know how to write enough code to just write the script out myself. I believe I’ll need to have to establish a variable: the column name (not sure if I can extract this using the regex code I found out) and then use that in the offset...
But my range needs to be dynamic and loop…
Please let me know if you can assist – I’ve been stuck on this for like a week!
Thank you all so much for reading – if I can provide extra detail please let me know.
For example you could do it this way:
Sub Tester()
Dim arr, i As Long, c As Range, v, col, price
For Each c In Range("G2:G4").Cells
v = Replace(Replace(c.Value, "{", ""), "}", "") 'remove braces
If Len(c.Value) > 0 Then 'anything to process?
arr = Split(v, ";") 'split on ;
For i = 0 To UBound(arr) - 1 Step 2 'loop 2 at a time
col = CLng(Trim(arr(i))) 'column number
price = Trim(arr(i + 1)) 'price
c.Offset(0, col).Value = col & ";" & price
Next i
End If
Next c
End Sub
Thank you in advance for your help.
The short version:
Need to have it so specific cells in a table are locked based on the what the first cell in that row contains.
The cell that is used to determine if other cells in the row are locked needs to always remain unlocked
I am working on an Excel sheet that contains a table. This table is used to record key information about products and is being updated daily. Each product is defined in its own row with columns being used to show details such as "Drawing number" and "Customer" etc.
Depending on the stage the product is at in development and how well the product went once completed, it will be labeled with a single "state" from a list such as "Ongoing", "Bad-product", "Archived" etc.
enter image description here
In each cell in each row, the text the cell contains is determined through either data validation in the form of lists or manually typed in. for example, all cells in the customer column will use a dropdown menu to select a customer from a list, but the cells in the drawing number column are always one of a kind so require manual input.
As so many people access and edit this table there are issues with mis-clicking and typing which causes data to be overwritten without someone noticing they have done so.
Because of this issue I want to be able to lock specific columns in a row based on what the first cell in the row contains ("State" column).
In the instance that the state column contains the word "archived" I want to lock the entire row from having its contents being changed.
In the case of the word being "Ongoing" I want to lock column AA and AB in that row.
As it is sometimes is necessary to go back and edit information due to a known mistake I would need to have it so the State column always remains unlocked whatever it contains.
I have attached a redacted Excel sheet that contains my attempt at a coded solution.
https://drive.google.com/open?id=1rzTp0ur1tpXIY_Wa3cVlcwruJ7Q1rT
The code can also be seen below.
Note: I was not sure how to even go about fulfilling the requirement for the "Ongoing" condition so would really appreciate it if someone could point me in the right direction
Sub Locking()
Dim KeyCells As Range
Set KeyCells = Range("A3:A612")
For Each cell In Range("A3:A612")
If cell.Value = "Archived" Then
cell.EntireRow.Locked = True
ElseIf cell.Value = "Ongoing" Then
cell.EntireRow.Locked = False
ElseIf cell.Value = "Bad" Then
cell.EntireRow.Locked = False
End If
Next cell
End Sub
This is my first post on here as I could not find the solution from looking at several different stack overflow questions. I also watched numerous videos on VBA code, but am definitely missing something, and am struggling to figure out what it is.
If I have posted this incorrectly or extra detail would be helpful, please let me know.
Any and all help is extremely appreciated!
Thank you
Edit: Code works now
Instead of locking the entire row
cell.EntireRow.Locked = False
if you just want to lock AA and AB in that row do the following:
cell.EntireRow.Locked = False 'unlock entire row first
'then lock just the 2 rows
Range("AA" & cell.Row & ":AB" & cell.Row).Locked = True
The others would work the same way.
I am trying to write a script to take an easy look into my data. The data is structured as follows:
Row 1: Parameter Name
Row 2: Lower Limit
Row 3: Upper Limit
Row 4: Unit
Row 5 and below: data (can go up to a couple thousands lines and couple hundred columns).
The thing I want to achieve is a script which formats each cell from row 5 down, to color green if it is in between the limits, and to color red if it is not. Each cell should look to it's own column row 2 and 3 for the limits.
I have tried going cell per cell, or column by column. Both worked fine on smaller datasets, but showed problems (excel freezing and eventually closing) on bigger datasets.
I am now trying to format a complete range (because excel has no problems when I do a big range in one piece by hand whatsoever) at once, but I can't access the individual column properties.
The code I am using:
With formatRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="=" & Cells(2, formatRange.Column).Address, Formula2:="=" & Cells(3, formatRange.Column).Address
.FormatConditions(1).Interior.Color = RGB(0, 249, 49)
End With
Now say my range is from A5:B10.
formatRange will be A5:B10.
I would expect every cell from A5:A10 to compare their values against A2 and A3. This is indeed the case
But the cells of B5:B10 also compare their values against A2 and A3.
So my question is, is there a scalable way I can make range B5:B10 look at B2 and B3 instead?
Edit
The answer of #Ryan B. is an easy and correct way of doing it by hand.
The problem in vba turned out to be the following:
Formula1:="=" & Cells(2, formatRange.Column).Address would ultimately result in Formula1:="=$A$2"
As suggested by the accepted answer, this needed to change to Formula1:="=A$2" to work.
The solution I found was to create a function which cut of the first character, and create the correct formula this way.
This can be done by tricking Excel's absolute and relative referencing within conditional formulas. No VBA is going to be required. Here is a quick mock of how I understand your data:
Select the first cell of your 'Row 5' range -- where you're going to apply the conditional formulas.
Open the conditional formatting dialog from the Styles button group in the Home Ribbon (this is B5 in my mock-up),
Create a 'New Rule'
Choose 'Use a formula to determine which cells to format' rule type
Begin typing the following rule. You will have to be quite careful and avoid all use of the arrow keys. If you need to get to a different point in the formula, use your mouse to move the insertion point. Make the proper adjustment in your formula if that initial column isn't column 'B' in your worksheet
=AND( B5 >= B$2, B5 < B$3)
Pay special notice to the Dollar Signs. There are NO absolute references used for the target cell, B5. There are absolute references (that's the dollar sign) in front of the row numbers for Lower and Upper, but not on the columns.
Set you're desired look for the "in-bounds" formatting and select OK.
Repeat the steps for your "out-of-bounds" formatting. Use the formula
=OR( B5 < B$2, B5 >= B$3)
Finally, to apply the formulas to your entire range:
Select the cell with the prepared formulas (B5 in this example) and hit [ctrl] + [c] to put excel into cut/copy mode
Select the entire target range
Right click and take 'Paste Special' from the context menu
Paste as formats
And your formatting should propagate through the worksheet.
Hope it helps. Always experiment on a copy of your workbook :)
Maybe I have used the wrong phrase to search but I have not been able to find a solution to the following challenge for Excel/VBA:
In Sheet 1 I have a database of order data from cells A1 to F10. Each row contains data for one order. Column A contains the order number.
As first step I, in sheet 2, wish to make a search on the order number and retrieve all data for that order into cells A5 to F5 in sheet 2.
After reviewing the data (and running another macro to recalculate the sales price) I wish to have the revised data replace the original data in sheet 1.
Can anyone help me with this?
Thank you in advance!
The 1st part does not need macro.
vlookup (fkeres magyarul) is your function. If you put this function in A5-F5 fields in sheet 2, looking for a given order number, in Sheet1 $A$1:$F$10, entering the required column number, asking exact match (FALSE), it works nicely. I entered the functions parameters in my previous sentence.
The 2nd part needs macro which makes Copy Paste back.
OR
Look for the requested order row in sheet2 A5 cell with MATCH (HOL.VAN magyarul) function, like =MATCH(A1;Sheet1!A1:A10;0)
and put INDIRECT (INDIREKT magyarul) function, like =INDIRECT("Sheet1!B"&A5;TRUE) in B5, etc.
In this case the Copy-Paste back macro is (starting from Sheet2):
Myorder = Range("A5").Value
Range("B5:F5").Select ' A5 is the row number of order, not the order No
Selection.Copy
Application.Goto Reference:=Worksheets("Munka1").Range("A" & Myorder)
ActiveSheet.Paste
...or very similar
I work in sourcing and I'm trying to look through a list of cells that contain part numbers and their description, but only pull the part number from the cell and place it in another column.
The part numbers have one of 10 possible prefixes (i.e. ABC, DDA, GHF, AH, etc.). The cells may look something like this:
Tire Pressure ABC123873 Monitor
Oil Life ABC849999999021 gauge
Air conditioner GHF211 maintenance
And I want to be able to search that entire list and only pull the following information into another column:
ABC123873
ABC849999999021
GHF211
As you can see from above, the challenge is that the part numbers are all different lengths and have no particular convention to them. The only thing you know about them is that they can have one of ten possible prefixes as I mentioned above (ABC, GHF, etc.).
The current solution I have looks something like this:
C2=FIND("ABC", A2)
D2=FIND(" ", A2, C2)
E2=MID(A2, C2, D2)
Where cell A2 contains the complete part number and description, C2 finds the beginning location of the part number by searching for its prefix, D2 finds the ending location of the part number by searching for a space, and then E2 pulls the substring between those two locations.
As you can see, this isn't a very robust solution and it only allows me to search for parts that start with ABC. I want to be able to search for all 10 possible prefixes (ABC, DDA, GHF, AH, etc.) but that does not work. I tried the approach below:
C2=FIND({"ABC", "DDA", "GHF", "AH"}, A2)
But that only searches for the ABC parts and disregards the other prefixes. Any help you all can offer will be greatly appreciated!!
This is how I would do it. I created a list of prefixes to search in column G. You could even put them on a separate page to keep it cleaner.
=MID(A1,FIND(G1:G2,A1,1),FIND(" ", A1, FIND(G1:G2, A1, 1))-FIND(G1:G2,A1,1))
I only tested two prefixes but just change G1:G2 to G1:G10 and put all you prefixes in them. The formula looks in A1. The first FIND finds the beginning character number. The rest of the equation is calculating the end of the product number minus the beginning which returns the amount of characters to return. Its important to know that MID isn't wanting the beginning and end character. It wants the beginning character and the number of characters after that to return. So FIND(" ", FIND(G1:G2, A1, 1)) does not work as it returns too many characters. Give it a try.
Oh and don't forget to do Ctrl+Shift+Enter to enter the array formula
The VBA answer would be
Sub findProductCode()
' Variable Declarations
Dim productName As String
Dim productArray() As String
Dim wordCount As Integer
' Sets the active sheet to use
' Change Sheet1 to the name of your sheet
With Sheets("Sheet1")
' Selects the first cell to check
' Change A1 to the first cell with data
Range("A1").Select
' Loops through all rows until an empty row
' It will end if you have any empty rows in the midle of data
Do Until IsEmpty(ActiveCell)
' Reads text out of the cell
'Change A to the proper column
productName = Range("A" & ActiveCell.Row).Text
' Splits the string into individual words
productArray = Split(productName, " ")
' Loops through array to find the product number
For wordCount = LBound(productArray) To UBound(productArray)
' Check if the word has the desired 10 prefixes
' Add more Or Left(productArray(wordCount), 3) = "xxx" until you have 10 of your prefixes you need
If Left(productArray(wordCount), 3) = "ABC" Or Left(productArray(wordCount), 3) = "GHF" Then
' Sends the product number to its cell
' Change B to the correct destination Cell
Range("B" & ActiveCell.Row).Value = productArray(wordCount)
End If
Next
' Increments the active cell to the next cell down
ActiveCell.Offset(1, 0).Select
Loop
End With
End Sub
If you don't know how, you need to enable the developer tab in excel. File->Options->Customize Ribbon Add the Developer to the Ribbon. The save your worksheet as .xlsm It is the macro enabled workbook. Then go to the developer tab, Choose "Visual Basic" and double click "ThisWorkBook". Paste the code in. You can run the code from there for a one time deal or you can create a button on the sheet that you can run this macro whenever you want.
To add a button, go back to the sheet you want the button on. On the developer tab select Insert and choose the first form control Button. Draw the button on your sheet and a dialog box will appear. select the findProductCode macro and select OK. Now every time you click the button it will run the macro.
This isn't the prettiest formula, but it should get you on your way. There's definitely a VBA solution, if you're interested in a macro instead of a formula.
Assuming your Tire Pressure ABC123873 Monitor is in A1, enter this into B1:
=MID(SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1),SEARCH("ABC",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1)),SEARCH(" ",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1))-SEARCH("ABC",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1)))
How it works, is it takes your A1, and first uses Substitute. I substitute the first and second [space] for ; and -. (Note, if there are more spaces than two, this will run into issues). Then, I just use Mid to extract the part of the cell starting with ABC and going until it hits the space. If you don't have "ABC" starting the values you want, you need to adjust the Search("ABC"... part (or use an array there). I am still working on it, but hopefully this helps give an idea.