Excel VBA find part number from indented BOM [closed] - vba

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
We have a software (Solidworks) from wich we extract a Bill of Materials in an Excel spreadsheet.
It returns the following data:
I would like to create a VBA macro that populates column C (parent) with the parent part number. For exemple cell C6 would display : 101-07798-111.
I managed to do it with an Excel formula directly in the worksheet, however I would like to do it with a VBA macro.
The excel formula requires 2 columns.
"Column D" in wich I do a concatenate of a letter and the data of "column A".
"Column E" wich does an Index(match) search of "column A" data to return the value of "Column B".
Column D formula : =CONCATENATE("A";A3) *without this step the main formula have errors
Column E formula : =INDEX($B$1:$B$250;MATCH((IFERROR(LEFT(D3; FIND("$"; SUBSTITUTE(D3; "."; "$"; LEN(D3)-LEN(SUBSTITUTE(D3; "."; ""))))-1);"-"));$D$1:$D$250;0))
I found ways to have a VBA script populate the rows with the formula; however since the formula contain a lot of " it causes error in the script.
What could be the best way to use the data in "column a" to get the value of "column B" in a vba script?
Thank you

I figured what the heck I want to figure this out so this is how I would do it.
Dim splitVariable As Variant
Dim level As Integer
Dim stringToFind As String
For Each cell In Range("A1:A" & [A1].End(xlDown).Row)
splitVariable = Split(cell.Value, ".") 'split the cell on the period to find the level
level = UBound(splitVariable) + 1 'add one because array is 0 indexed
If level > 1 Then 'don't do anything for the first level
stringToFind = Left(cell, level - 3 + level) 'get the level of the parent
For Each parentCell In Range("A1:A" & [A1].End(xlDown).Row) 'loop through rang again to find level
If parentCell.Value = stringToFind Then 'when the parent is found then paste it to column C
cell.Offset(0, 2) = parentCell.Offset(0, 1)
Exit For
End If
Next
End If
Next
don't know if that helps at all.

Related

Excel VBA programming range selection questions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
NEED HELP DOING THIS PLEASE:
Opens an Input box and prompts the user to choose a worksheet number from 1 to
Stores the value the user entered in a variable of type integer named Index.
You may assume the user will always enter a valid input (i.e. 1, 2 or 3). The
program will give run time error if the user enters text for example- but you do
not need to write code to handle this case.
Uses the index number entered by the user and the Worksheets( ) collection object
to activate the selected worksheet.
Find the number of items in the list in column A (starting in cell A1) and store it
in a variable named L1; find the number of items in the list in column B (starting
in cell B1) and store it in a variable named L2.
Use an If statement to determine what string to store in a variable named
Answer:
“ List 1 is longer” if the number of items in column A is larger.
"List 2 is longer" if the number of items in column B is larger, and
"Same length" otherwise.
Open a message box showing Answer.
Well...
Sub HomeworkForNmHomie13()
Dim Response, Index, L1, L2, Answer
Do
Response = InputBox("Enter a number from 1 to " & Worksheets.Count)
If Response = "" Then Exit Sub
'Your teacher said don't do error handling, but that's for failures.
On Error Resume Next
Index = Int(Response)
On Error GoTo 0
If Index > Worksheets.Count Or Index < 1 Then
MsgBox ("Your entry was invalid. Please enter a number between 1 and " & Worksheets.Count)
End If
Loop While Index > Worksheets.Count Or Index < 1
Sheets(Index).Activate
L1 = Cells(Rows.Count, "A").End(xlUp).Row 'Assuming an "item" includes blank cells
L2 = Cells(Rows.Count, "B").End(xlUp).Row 'Just grab the last row with data
'Use 2 IIF Statements to check the length using one line of code and look smart as hell
Answer = IIf(L1 > L2, "List 1 is longer", IIf(L2 > L1, "List 2 is Longer", "Same length"))
MsgBox (Answer)
End Sub
If you would prefer to "impress" your teacher by meeting the minimum requirements:
Sub LazyHomeworkForNmHomie13()
Index = Int(InputBox("Enter a number from 1 to " & Worksheets.Count))
Sheets(Index).Activate
L1 = Cells(Rows.Count, "A").End(xlUp).Row
L2 = Cells(Rows.Count, "B").End(xlUp).Row
Answer = IIf(L1 > L2, "List 1 is longer", IIf(L2 > L1, "List 2 is Longer", "Same length"))
MsgBox (Answer)
End Sub

Excel VBA: locate details in one sheet, then copy (append) to another [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would really appreciate your help with building some macro logic. I've messed around modifying recorded macros in the past, but am not familiar enough with VBA to build something from scratch. I've looked over the many examples here as well (mindboggling!), but did not find anything close enough to what I need to do...
I know it probably only takes a few lines of code - which makes it even more frustrating. Can someone please put me on the right track?
I have a single workbook with two sheets. Sheet1 contains a long list (~25k rows, but variable) of product details. Column A has the product ID, then columns B through G hold specific details on each product. Sheet2 is similar, again with product ID in column A and (different) properties in columns B through E. Sheet2 is much smaller, at about 100 rows (also variable).
What I need to do is to loop through the products (rows) in Sheet2, find the corresponding Product ID in Sheet1, and copy/paste the product properties in Sheet1 (B through G) to Sheet2 (to the right of the existing properties, so starting at column F in my example) - effectively merging all product properties in Sheet2.
I would be very grateful if one of you wizzards can provide skeleton code for that....
Set the two ranges from your two sheets, loop through them and find matches in product id, then just copy the values from the first sheet into the second one. Also replace the ranges to match your conditions.
Sub search()
Dim cell As Range, rng As Range, rng2 As Range, cell1 As Range, n As Integer, m As Integer
Set rng = Sheet1.Range("A2:A9")
Set rng2 = Sheet2.Range("A2:A3")
n = 1
m = 1
For Each cell In rng
n = n + 1
For Each cell1 In rng2
m = m + 1
If cell.Value = cell1.Value Then
Sheets("Sheet2").Range("F" & m & ":J" & m).Value = Sheets("Sheet1").Range("B" & n & ":F" & n).Value
End If
Next cell1
m = 1
Next cell
End Sub

How to find the standard deviation of alternative cells in excel 2007 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to calculate the standard deviation of alternative cells in a 10000 rows in excel 2007, how can i do that ? whether i need a formula or a VBA ?. Can some help me.
It's also possible with a formula. This "array formula" gives you standard deviation of every other cell starting at A1
=STDEV(IF(MOD(ROW(A1:A10000)-ROW(A1),2)=0,A1:A10000))
confirm with CTRL+SHIFT+ENTER
I would use both, the following code splits/sorts your rows and selects every second row and pastes it into another column, beginning with the first cell:
Sub splitcells()
Dim rows As Long
Dim i, j As Long
j = 0
rows = ?1?
For i = ?2? To rows Step 2
Cells(i - j, ?4?).Value = Cells(i, ?3?).Value
j = j + 1
Next
End Sub
Then you have to replace:
?1? - with the end row number of your list
?2? - with the beginning row number of your list
?3? - the column number of your list to be sorted
?4? - the column number of your destination column (the one the sorted data is sent to)
Once you have completed this, just run a normal standard deviation function on your new column and you'll have your answer.

VBA Excel- how to know the layout of data in a range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am an Excel VBA newbie, though I have several years of programming experience.
I am trying to write a VBA function in excel where a user can input several cells in an application.inputbox by selecting individual cells, a cell range, or a combination of both. Once input, I want to check the values for errors etc.
I can input the range without problem, but seem to need a way to inspect the range to determine its layout so I can find the values. It seems the location of the values varies with how the user selected the cells.
Suppose the user selects 4 cells. A1, A2, c2, f5 vs drag selecting A1:A2, and then clicking C2,f5.
in both cases Inputs.Count is 4.
In the first case Inputs(1)- inputs(4) do not return the selected values.
Here, in the first case, the values appear as single cells in 4 different Areas.
In the second case, Inputs(1)-Inputs(4) do return the selected values, yet these appear as three areas. One area with two cells (two rows, one column), and two areas with one cell each.
I've two questions-
1: Is there an easy way to determine where the data I want to inspect are (i.e., the layout of the range) without checking all the area/cell counts?
2: Where can I find an accessible read on using ranges.
Best,
Byron
To compare a few methods after selecting A1,A2,B3,D4
Sub tester()
Dim c As Range, a As Range, x As Long, s
Debug.Print Selection.Areas.Count
Debug.Print "###By cell count"
s = ""
For x = 1 To Selection.Cells.Count
s = s & " " & Selection.Cells(x).Address()
Next x
Debug.Print s
Debug.Print "###For Each cell"
s = ""
For Each c In Selection.Cells
s = s & " " & c.Address()
Next c
Debug.Print s
Debug.Print "###Looping through areas"
s = ""
For Each a In Selection.Areas
For x = 1 To a.Cells.Count
s = s & " " & a.Cells(x).Address()
Next x
Next a
Debug.Print s
End Sub
Output:
###By cell count
$A$1 $A$2 $A$3 $A$4
###For Each cell
$A$1 $A$2 $B$3 $D$4
###Looping through areas
$A$1 $A$2 $B$3 $D$4
Only method #1 is unreliable - the others give consistent results however the range was originally selected.

EXCEL: Automating spreadsheet data input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am currently doing some data entry for a spreadsheet which contains hundreds on entries and want to automate the process, I have a good idea of what I want it to do but have little experience with Excel or VBA.
The idea behind it is that I have a code in one column and in the next column there is another code which is unique to the value in the former column. To give an example:
So for every cell that contains 123, the column next to it will be "ABC".
The sort of solution I would like is a macro that will work its way down Column A, storing the value of each cell (or something of that effect) and then working its way down to check for values that match that stored one. If a match is found, the macro will then copy the code from column B, the cell that is next to the stored cell and copy it into the cell in column B, next to the match.
EXAMPLE:
It will store the "123" value in A, work its way down Column A to find other cells matching "123" and when it finds them copy "ABC" into the column B cells next to the matches.
Hope this is easy to understand and someone can help me with coming up with a solution, would make this whole process alot easier as the spreadsheet is growing by the day and manual input is taking far to much time
Give this macro a try:
Sub FillInTheBlanks()
Dim rA As Range
Dim rB As Range
Dim r As Range, rr As Range
Dim N As Long
Dim va As Variant
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rA = Range("A1:A" & N)
Set rB = rA.Offset(0, 1).Cells.SpecialCells(xlCellTypeBlanks)
If rB Is Nothing Then Exit Sub
For Each r In rB
va = r.Offset(0, -1).Value
For Each rr In rA
If rr.Value = va And rr.Offset(0, 1) <> "" Then
r.Value = rr.Offset(0, 1).Value
End If
Next rr
Next r
End Sub