I am building a web scraping tool that obtains particular data. Once the data has been extracted the next step is to summarize it into a report thus i need some guidance on the final part of the project.
I have a column (Column A) that contains the following data set
Description of product
$3000
Description of product
$5000
etc
I would like to find a value (in this case the common value is $) and cut this value next to the description (into Column B). There could be hundreds of rows thus a loop would be required.
My initial thought is to use code that will find a value ($), then once the value is found, cut the row and using an offset paste the value (into column B)
Any help would be appreciated
sub test()
dim usedrows,i as integer
usedrows = activesheet.range("A" & activesheet.rows.count).end(xlup).row
for i=0 to usedrows
if instr(range("A" & i+1),"$") <> 0 then
'Checks if the looped cell has "$" sign
range("B" & i+1)=range("A" & i+1)
range("A" & i+1)=""
end if
next
end sub
Copy ColumnA into ColumB. Delete B1 with Shift cells up. Series fil1 a column with 1 in odd rows, 2 in even, then filter to select the 2s delete those rows and then the column of 1s.
Related
first of all, thank you all, your questions and answers have been of great value over the years, but now i've run into a problem of which I'm just not seeing/finding the solution.
Edit: before i even asked the question: solved big part of my question already, but now i have 1 issue remaining. I'll leave the details below in, just for clarity.
Document layout:
Sheet1: Overview (gets renewed every week or so, on command).
Contains array of (text) data A:Z, approx 3k lines.
Column A contains the unique identifier, for the contents of said row.
Sheet2: Backup (contains exact backup, pre-update of Overview)
Goal:
Loop through all unique identifiers (Col.A) and test B:Z with respect to B:Z on Sheet2, the backup, with an INDEX/MATCH. If the value of the backup is different, add that value as a Note(comment) to the ActiveCell.
Created a small testworkbook, with the following data on Sheet1 -and- Sheet2:
Unique
V1
V2
V3
V4
212223
B
C
D
E
212224
F
G
H
I
212238
J*
K
L
M
*Cell B4 on Sheet1 was updated to "Z".
Now i got pretty much everything working, except my comparing the ActiveCell offset to the Index/Match result. I'm looping through my range in Col A, checking each following column for it's matching value on Sheet2. When i arrive at the following point in the code, my activecell is A2. Condensed down, looks like this:
Sub CellCheck()
Dim NewNote As Variant 'Note-Content to be
Dim ActiveCell_Debug As Variant
Dim col As Integer 'Starting Column Counter
col = 1
'Start at cell A2
ActiveCell_Debug = ActiveCell.Address
If ActiveCell.Offset(0, col).Value <> "INDEX(Sheet2!$B:$B, MATCH(Sheet1!" & ActiveCell_Debug & ",Sheet2!A:A,0))" Then
NewNote = Application.Evaluate("INDEX(Sheet2!$B:$B, MATCH(Sheet1!" & ActiveCell_Debug & ",Sheet2!A:A,0))")
ActiveCell.Offset(0, col).AddComment
ActiveCell.Offset(0, col).Comment.Text Text:=NewComment
'ActiveCell.Offset(0, col).DisplayCommentIndicator = -1
End If
End Sub
Now "ActiveCell.Offset(0, col).Value" is "B".
And "INDEX(Sheet2!$B:$B, MATCH(Sheet1!" & ActiveCell_Debug & ",Sheet2!A:A,0))" is -also- "B".
But 'NewNote' is now also "B".
If i make Cell A4 the Activecell, result is "J" as expected. So, no matter what the test is always true.
What am i missing here?
Many thanks in advance.
I'm trying to search on the specific column(E), and if matched with the first 4 digit, I would like to copy the number to a different column.
Column E is where i would like to paste all the random number(dynamic)
Column A/B/C is static where i would add 4 digits from time to time.
Column I/J/K is where is would like to paste the result.
PS:
I'm doing it manually and would really appreciate if someone can help me out with the automation hence no code is provided. :(
Having ExcelO365 means you may use FILTER(). Therefor try the below:
Formula in I2:
=FILTER($E:$E,ISNUMBER(MATCH(--LEFT($E:$E,4),A:A,0)))
Drag right to K2. Now, this is dynamic and will change accordingly upon data entry in column E:E, or changing values in A:C.
this is the code to execute on sheet 1, it goes through the entire column E and validates through the formula of counting if in each of the first three columns and assigns the value found in the corresponding columns.
Sub macro()
Dim Static_Data As String
Dim Sht As Worksheet
Set Sht = ThisWorkbook.Sheets("Hoja1")
Active_row = 2
Do While Sht.Range("E" & Active_row).Value <> ""
Static_Data = Sht.Range("E" & Active_row).Value
For i = 1 To 3
If Application.WorksheetFunction.CountIf(Sht.Columns(i), Mid(Static_Data, 1, 4)) > 0 Then
Sht.Cells(Sht.Cells(Rows.Count, i + 8).End(xlUp).Row + 1, i + 8).Value = Static_Data
End If
Next i
Active_row = Active_row + 1
Loop
End Sub
For Excel versions that don't support FILTER or as an alternative you can use standard formulas for this.
If you use columns F-H as helper columns (and these columns can be hidden) then the formula in F2 will be:
=IF(NOT(ISERROR(VLOOKUP(VALUE(LEFT($E2,4)),A$2:A$100,1,FALSE)))=TRUE,$E2,"")
The formula can then be copied across and down. This will find your matches.
In order to then remove the blanks from the data you can use the following formula in I2 and again copy across and down. Depending on how many numbers you want to add in, you may want to extend the range A$2:A$100 in the top formula and F$2:F$100 in the bottom formula
=IFERROR(INDEX(F$2:F$100,AGGREGATE(15,6,(ROW(F$2:F$100)-ROW(F$2)+1)/(F$2:F$100<>""),ROWS(I$2:I2))),"")
Is there a way to concatenate multiple columns if the a row is duplicate? I have a spreadsheet where column A has duplicate team but there area and LD (column b and c) are different value. I would like to have a formulate at column E where it will concatenate column B and C with dash and append next row values. See the attached picture highlighted row E. Any idea how to do this with excel formula or may be VBA. I tried this formula in column E =IF(A3=A4,D3&";"&D4) but it returns false for the last duplicate row.
This is not possible with formulas. It requires a VBA-based solution.
I wrote a custom routine for you. Please place this in a standard code module:
Public Sub ConcatTeamZones()
Const SOURCE = "A1"
Const OUTPUT = "E1"
Dim i&, j&, s$, v, w
v = Range(SOURCE).CurrentRegion
ReDim w(1 To UBound(v), 0)
For i = 2 To UBound(w)
If v(i, 1) <> v(i - 1, 1) Then
w(i - 1, 0) = s
s = s & v(i, 2) & "-" & v(i, 3)
s = ""
Else
s = s & ";"
End If
s = s & v(i, 2) & "-" & v(i, 3)
Next
w(i - 1, 0) = s
Range(OUTPUT).Resize(UBound(w)) = w
End Sub
And then from the worksheet with your team data, press ALT-F8 to bring up the Macro Dialog. Run the ConcatTeamZones macro.
Note 1: this assumes that column A is sorted.
Note 2: You can edit the first two lines to specify which columns contains the source (team data) and which column you wish the output.
It can be done using formulas, it’s just a matter of perspective:
Assuming data is sorted by Team
This formula gives the concatenated result with the maximum of combinations on top. Enter this formula in cell E2 and copy till last record.
=CONCATENATE($D2,IF(EXACT($A2,$A3),";"&$E3,""))
To assign the max possible combinations to each Team enter this formula in F2 and copy till last record.
=INDEX($E:$E,MATCH($A2,$A:$A,0),0)
Here's how I would do it...
Cell "A1": =COUNTIF(B$2:B2,B2)&B2 - This is to create a unique key. Copy down the length of your table
Then I would use an advanced query (with vba maybe) to create a list of unique values for team in the "F" column
Cell "G2": =VLOOKUP("1"&F2,A:D,3,0)&"-"&VLOOKUP("1"&F2,A:D,4,0)&IF(ISERROR(VLOOKUP("2"&F2,A:D,3,0)),"",", "&VLOOKUP("2"&F2,A:D,3,0)&"-"&VLOOKUP("2"&F2,A:D,4,0))&IF(ISERROR(VLOOKUP("3"&F2,A:D,3,0)),"",", "&VLOOKUP("3"&F2,A:D,3,0)&"-"&VLOOKUP("3"&F2,A:D,4,0))&IF(ISERROR(VLOOKUP("4"&F2,A:D,3,0)),"",", "&VLOOKUP("4"&F2,A:D,3,0)&"-"&VLOOKUP("4"&F2,A:D,4,0))
This function creates your combined references. It would be longer if you expected more than 4 occurrences of teams.
Just copy "IF(ISERROR(VLOOKUP("4"&F2,A:D,3,0)),"",", "&VLOOKUP("4"&F2,A:D,3,0)&"-"&VLOOKUP("4"&F2,A:D,4,0))" and change the "4" to "5" etc
You could hide column A (to tidy up).
Sorry, I tried to include an image but insufficient reputation :-)
I've been looking for it everywhere but I can't seem to find the answer although it seems an regular problem.
I'm trying to automate the duplication of cells in excel.
I have two lists: list 1 with values 1,2,3,4 and the second list is with values a,b,c,d.
Now I want to have a file where for every value in list 1, the values in list two are duplicated in excel. So:
1 - a
1 - b
1 - c
1 - d
2 - a
2 - b
2 - c
2 - d
3 - a
...
I'm wondering if there's a function within excel or if not a macro that I could use to solve this? For this short list it is of course easy to do with autofill, but when the list consists of a few hundred values, it gets more complicated...
You can do this also with a few formulas/support columns without VBA:
Let's assume your first category is in column A, starting in A2 and your category is in column B.
Determine the record count for each category, e.g. in C1: =COUNTA($A:$A)-1 (assuming a header row) and C2 equivalent
Place two support column, e.g. in E and F) - E will hold the row for the first category and F the Id for the second category. Place the following formula in E2: =IF(ISTEXT(E1),1,IF(F2=1,E1+1,E1)) and this in F2: =IF(ISTEXT(F1),1,IF(F1=$C$2,1,F1+1))
Add two more columns for the final result - G for category one with formula =INDEX(A:A,E2+1) and H for category 2 with the formula =INDEX(B:B,F2+1).
Now simply copy the formulas for the columns E:H down for a many rows as required (number of rows required is =C1*C2
In the end it'll look something like this:
You can download the file here.
This should be really easy to understand ...
Open VBE ALT+F11 and right click VBA project. Insert a Module and copy-paste the below code. Hit F5 to run
Sub Main()
Dim number As Range
Dim letter As Range
For Each number In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
For Each letter In Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
With Sheet2
.Range("A" & .Range("A" & Rows.Count).End(xlUp).Row + 1) = number
.Range("B" & .Range("b" & Rows.Count).End(xlUp).Row + 1) = letter
End With
Next
Next
End Sub
The above code assumes that your sheet1 looks like this
and the results will be on Sheet2 like this
I would like to know how to write a Macro to copy the value of L3 up to L2 based on the fact that A2 and A3 are equal and B2 and B3 are also equal. Where subsequent rows of columns A and B are not equal the macro does not need to push data up. In some instances there may be as many as 10 subsequent rows where A & B column vales are equal and the oldest month value would need to roll right up to the top row where A & B match.
The entire sheet has about 150 rows with many City and Company values. I am currently performing this operation manually and it is time consuming. The purpose of the spreadsheet is to provide a sales visit summary based on data that is extracted out of our SQL based CRM package. Row 4 has no meaningful data is that context but it does not hurt if the data is rolled up Row 2 as long as all months with visits are represented on the top row where columns A & B match.
I'm guessing that the macro needs to loop through JAN (Column E) and complete that column and then loop through columns G through P sequentially after that. Only Columns E through P need to be manipulated. I don't think it will have any negative consequences if the data is concatenated in the cells or just overwritten. Once the Macro is complete the top row for a given Company at a certain site should contain values in all of the months where it was visited.
My programming knowledge is limited but I can usually reverse engineer code enough to modify it if I get a good sample. I hope my request is clear.
Based on your first initial question "how to copy L3 to L2 if B2=B3 and A2=A3", this basic macro will get you started along those lines:
Option Explicit
Sub CopyUP()
Dim LR As Long, Rw As Long, col As Long
LR = Range("A" & Rows.Count).End(xlUp).Row 'last row with data
For Rw = LR To 2 Step -1 'from the bottom up, compare
If Range("A" & Rw) = Range("A" & Rw - 1) And _
Range("B" & Rw) = Range("B" & Rw - 1) Then
For col = 5 To 16 'columns E:P
If Cells(Rw, col) <> "" Then Cells(Rw - 1, col) = Cells(Rw, col)
Next col
End If
Next Rw
End Sub