My program appears in the VBA screen to be working, but on the Excel doc it's manipulating, not so much.
It is supposed to loop down a root column, populating off of it to other sheets, until the root column comes to a blank cell. When I run the loop, it does loop, and the plus 1 shows that it took effect for RW variable, when I hover over it in the VBA stepo through process. Yet it just keeps entering values from the same row over and over again as if RW isn't advancing when I can see that it is, and it keeps copying to the same row, when again, I can see the RW variable advancing by 1 in the VBA editor.
Here's the code.
Sub ExportData()
Dim RW As Integer
RW = 2
Dim CL As Integer
CL = 3
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "US"
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "CA"
End With
Dim SKUUS As Range
Set SKUUS = Sheets("US").Cells(RW - 1, CL - 2)
Dim SKUCA As Range
Set SKUCA = Sheets("CA").Cells(RW - 1, CL - 2)
Dim RootSKU As Variant
Set RootSKU = Sheets("tblTEMP").Cells(RW, CL)
AvailDate = InputBox("What is Available Date?")
PromoCode = InputBox("What is the Promo Code?")
'HeadersUS
SKUUS.Value = "Item #"
SKUUS.Offset(0, 1).Value = "MAX QTY"
SKUUS.Offset(0, 2).Value = "PROMO Price"
SKUUS.Offset(0, 3).Value = "AvailableDate"
SKUUS.Offset(0, 4).Value = "VendorNumOverride"
SKUUS.Offset(0, 5).Value = "PromoListPrice"
SKUUS.Offset(0, 6).Value = "BOGO ITEM #"
SKUUS.Offset(0, 7).Value = "BOGO QTY"
SKUUS.Offset(0, 8).Value = "ProgramCode"
SKUUS.Offset(0, 9).Value = "PromoCode"
'HeadersCA
SKUCA.Value = "Item #"
SKUCA.Offset(0, 1).Value = "MAX QTY"
SKUCA.Offset(0, 2).Value = "PROMO Price"
SKUCA.Offset(0, 3).Value = "AvailableDate"
SKUCA.Offset(0, 4).Value = "VendorNumOverride"
SKUCA.Offset(0, 5).Value = "PromoListPrice"
SKUCA.Offset(0, 6).Value = "BOGO ITEM #"
SKUCA.Offset(0, 7).Value = "BOGO QTY"
SKUCA.Offset(0, 8).Value = "ProgramCode"
SKUCA.Offset(0, 9).Value = "PromoCode"
Do
'Populate First Row in US
SKUUS.Offset(1, 0) = RootSKU.Value
SKUUS.Offset(1, 1) = RootSKU.Offset(0, 12).Value * 0.8
SKUUS.Offset(1, 1) = Math.Round(SKUUS.Offset(1, 1).Value, 0)
SKUUS.Offset(1, 2) = RootSKU.Offset(0, 7).Value
SKUUS.Offset(1, 3) = AvailDate
SKUUS.Offset(1, 5) = RootSKU.Offset(0, 6).Value
SKUUS.Offset(1, 9) = PromoCode
'Populate First Row in US
SKUCA.Offset(1, 0) = RootSKU.Value
SKUCA.Offset(1, 1) = RootSKU.Offset(0, 12).Value * 0.2
SKUCA.Offset(1, 1) = Math.Round(SKUCA.Offset(1, 1).Value, 0)
SKUCA.Offset(1, 2) = RootSKU.Offset(0, 10).Value
SKUCA.Offset(1, 3) = AvailDate
SKUCA.Offset(1, 5) = RootSKU.Offset(0, 9).Value
SKUCA.Offset(1, 9) = PromoCode
RW = RW + 1
Loop Until RootSKU = ""
End Sub
I moved the variable to inside the loop, but still get the same result. After playing around with it a bit on the premise, I was able to get it to work like this. Thanks for the assist.
Sub ExportData()
Dim RW As Long
Dim CL As Long
Dim SKUUS As Range
Dim SKUCA As Range
Dim RootSKU As Variant
RW = 2
CL = 1
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "US"
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "CA"
End With
AvailDate = InputBox("What is Available Date?")
PromoCode = InputBox("What is the Promo Code?")
'HeadersUS
Sheets("US").Range("A1") = "Item #"
Sheets("US").Range("B1") = "MAX QTY"
Sheets("US").Range("C1") = "PROMO Price"
Sheets("US").Range("D1") = "AvailableDate"
Sheets("US").Range("E1") = "VendorNumOverride"
Sheets("US").Range("F1") = "PromoListPrice"
Sheets("US").Range("G1") = "BOGO ITEM #"
Sheets("US").Range("H1") = "BOGO QTY"
Sheets("US").Range("I1") = "ProgramCode"
Sheets("US").Range("J1") = "PromoCode"
'HeadersCA
Sheets("CA").Range("A1") = "Item #"
Sheets("CA").Range("B1") = "MAX QTY"
Sheets("CA").Range("C1") = "PROMO Price"
Sheets("CA").Range("D1") = "AvailableDate"
Sheets("CA").Range("E1") = "VendorNumOverride"
Sheets("CA").Range("F1") = "PromoListPrice"
Sheets("CA").Range("G1") = "BOGO ITEM #"
Sheets("CA").Range("H1") = "BOGO QTY"
Sheets("CA").Range("I1") = "ProgramCode"
Sheets("CA").Range("J1") = "PromoCode"
Do
Set SKUUS = Sheets("US").Cells(RW, CL)
Set SKUCA = Sheets("CA").Cells(RW, CL)
Set RootSKU = Sheets("tblTEMP").Cells(RW, CL + 2)
'Populate First Row in US
SKUUS = RootSKU.Value
SKUUS.Offset(0, 1) = RootSKU.Offset(0, 12).Value * 0.8
SKUUS.Offset(0, 1) = Math.Round(SKUUS.Offset(0, 1).Value, 0)
SKUUS.Offset(0, 2) = RootSKU.Offset(0, 7).Value
SKUUS.Offset(0, 3) = AvailDate
SKUUS.Offset(0, 5) = RootSKU.Offset(0, 6).Value
SKUUS.Offset(0, 9) = PromoCode
'Populate First Row in US
SKUCA = RootSKU.Value
SKUCA.Offset(0, 1) = RootSKU.Offset(0, 12).Value * 0.2
SKUCA.Offset(0, 1) = Math.Round(SKUCA.Offset(0, 1).Value, 0)
SKUCA.Offset(0, 2) = RootSKU.Offset(0, 10).Value
SKUCA.Offset(0, 3) = AvailDate
SKUCA.Offset(0, 5) = RootSKU.Offset(0, 9).Value
SKUCA.Offset(0, 9) = PromoCode
RW = RW + 1
Loop Until RootSKU.Offset(1, 0) = ""
End Sub
Related
I have this code that transfers data to a sheet and creates a data base of : Name, DOB, Phone number, Insurance name, Insurance ID.
Dim strDataRange As Range
Dim keyRange As Range
Set strDataRange = Range("A1:h5000")
Set keyRange = Range("A1:h5000")
strDataRange.Sort Key1:=keyRange, Header:=xlYes
Dim tr As Worksheet
Set tr = Worksheets("Sheet16")
iRow = tr.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
tr.Cells(iRow, 1).Value = Me.TextBox1.Value
tr.Cells(iRow, 2).Value = Me.TextBox2.Value
tr.Cells(iRow, 3).Value = Me.TextBox22.Value
tr.Cells(iRow, 4).Value = Me.TextBox23.Value
tr.Cells(iRow, 5).Value = Me.TextBox17.Value
tr.Cells(iRow, 6).Value = Me.ComboBox15.Value
tr.Cells(iRow, 7).Value = Me.TextBox21.Value
tr.Cells(iRow, 8).Value = Me.TextBox32.Value
Then on this code, I am able to call all that information enter before (above code) and its populates on textboxes:
Private Sub ComboBox13_Change()
On Error Resume Next
Me.TextBox1.Value = Me.ComboBox13.Column(0)
Me.TextBox2.Value = Me.ComboBox13.Column(1)
Me.TextBox22.Value = Me.ComboBox13.Column(2)
Me.TextBox23.Value = Me.ComboBox13.Column(3)
Me.TextBox17.Value = Me.ComboBox13.Column(4)
Me.ComboBox15.Value = Me.ComboBox13.Column(5)
Me.TextBox21.Value = Me.ComboBox13.Column(6)
Me.TextBox32.Value = Me.ComboBox13.Column(7)
On Error GoTo 0
End Sub
With ComboBox13
.ColumnCount = 1
.ColumnWidths = "120"
.ColumnHeads = False
.RowSource = "Sheet16!A2:h5200"
End With
What I can't do or I need to do is, If the data change on any of these
enter code heretext boxes and I need to update the information such as
: phone 'number or Insurance Id. How can I change it on those text boxes
and press a 'command button to update that new data enter?
Thanks a lot
I am organizing a dirty text in an organised table. And this code stops when the cell the marked line is completed. Can you help me to make it continuing the loop?
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Dim sh7 As Worksheet
Dim CNAME As String
Set sh = Worksheets("Sheet6")
Set sh7 = Worksheets("Sheet7")
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
For n = 1 To lr
If InStr(1, sh.Cells(n, 1), "CALL:") = 1 Then
CNAME = sh.Cells(n, 7).Value
Ci = sh.Cells(n + 1, 7).Value
Cpd = sh.Cells(n + 1, 7).Value
Else
If InStr(1, sh.Cells(n, 1), "Topic:") = 1 Then
T = sh.Cells(n, 2)
Tpd = sh.Cells(n + 1, 2)
Types = sh.Cells(n + 4, 2)
DM = sh.Cells(n + 5, 2)
D = sh.Cells(n + 5, 4)
OD = sh.Cells(n + 6, 2)
lr7 = sh7.Cells(Rows.Count, 1).End(xlUp).Row
sh7.Cells(lr7 + 1, 1).Value = CNAME '********This is the last line it runs.
sh7.Cells(lr7 + 1, 2).Value = Ci
sh7.Cells(lr7 + 1, 3).Value = Cpd
sh7.Cells(lr7 + 1, 4).Value = T
sh7.Cells(lr7 + 1, 5).Value = Tpd
sh7.Cells(lr7 + 1, 6).Value = Types
sh7.Cells(lr7 + 1, 7).Value = DM
sh7.Cells(lr7 + 1, 8).Value = D
sh7.Cells(lr7 + 1, 9).Value = OD
End If
End If
Next n
End Sub
You should get in the habit of defining all variables and supplying a default value.
EDIT:
It seems my original conclusion was incorrect. Upon further inspection I see what might be an issue in your code. Both times where you are trying to get the last row, you are using Rows.Count as a parameter.
Maybe change these
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
lr7 = sh7.Cells(Rows.Count, 1).End(xlUp).Row
To this (note that I use the sheet variable in the first param)
lr = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
lr7 = sh7.Cells(sh7.Rows.Count, 1).End(xlUp).Row
I currently have a VBA script which generates a combined chart from some data. My manager has requested that the "grand total" column (a sum of all the other columns) be present in the data table below. However, he does not want it present in the graph itself. I know that were I doing this manually, I would be able to double-click the circled column and set its Fill to "No Fill," but I cannot figure how how to do this in VBA. Note I am not trying to hide the entire series, just the circled column in the picture below.
What I have:
Picture of Incorrect Chart
What I'm trying to accomplish:
Picture of Corrected Chart
Thanks for your time!
EDIT: Plotting Code:
'Plotting!
Dim dblMax As Double
dblMax = Application.WorksheetFunction.Max(dpws.Range("B2:P4"))
Dim chrt As Chart
Set chrt = pws.Shapes.AddChart.Chart
With chrt
.ChartArea.Left = 200
.ChartArea.Top = 0
.ChartArea.Height = 500
.ChartArea.Width = 800
.Legend.Position = xlLegendPositionBottom
.ChartType = xlColumnStacked
.HasDataTable = True
.SetSourceData Source:=dpws.UsedRange
.SeriesCollection("Forecasted % Complete").AxisGroup = 2
.SeriesCollection("Forecasted % Complete").ChartType = xlLineMarkers
.SeriesCollection("Forecasted % Complete").MarkerStyle = xlMarkerStyleSquare
.SeriesCollection("Cumulative").ChartType = xlLine
.SeriesCollection("Cumulative").Format.Line.Visible = False
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = dblMax + dblMax * 0.2
.Axes(xlValue, xlSecondary).MinimumScale = 0
.Axes(xlValue, xlSecondary).MaximumScale = 1
End With
And below you will find the full code.
Sub MyCode()
Dim dws As Worksheet
Dim pws As Worksheet
Dim start As Range
Dim dataRange As Range
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim startPvt As String
Dim lastCol As Integer
'Create ChartBin, ChartDate columns.
Set dws = Sheets("Sheet1")
With dws
lastCol = dws.Cells(1, .Columns.Count).End(xlToLeft).Column
.Cells(1, lastCol + 1).Value = "Chart_Bin"
.Cells(1, lastCol + 2).Value = "Chart_Date_Group"
End With
'Populate Chart Columns
Dim i As Long
Dim thisMonth As Integer
Dim hwswDateCol As Long
Dim statusCol As Long
Dim hwswDateGrpCol As Long
hwswDateCol = 162
statusCol = 13
hwswDateGrpCol = 163 'Really should search for these column titles.
thisMonth = Month(Date)
With dws
For i = 2 To .UsedRange.Rows.Count Step 1
.Cells(i, lastCol + 2).Value = .Cells(i, hwswDateGrpCol).Value
'If complete...
If (.Cells(i, statusCol) = "Complete") Then
.Cells(i, lastCol + 1).Value = "Complete"
'If not complete, date passed...
ElseIf (thisMonth - Month(.Cells(i, hwswDateCol)) > 0) Then
.Cells(i, lastCol + 1).Value = "Missed"
Else
.Cells(i, lastCol + 1).Value = "Forecasted"
End If
Next i
End With
'Copy just data we need to reduce pivot size.
Set rws = Sheets.Add
rws.Name = "Raw"
dws.Columns(1).Copy Destination:=rws.Columns(1)
dws.Columns(2).Copy Destination:=rws.Columns(2)
dws.Columns(4).Copy Destination:=rws.Columns(3)
dws.Columns(8).Copy Destination:=rws.Columns(4)
dws.Columns(10).Copy Destination:=rws.Columns(5)
dws.Columns(22).Copy Destination:=rws.Columns(6)
dws.Columns(131).Copy Destination:=rws.Columns(7)
dws.Columns(11).Copy Destination:=rws.Columns(8)
dws.Columns(101).Copy Destination:=rws.Columns(9)
dws.Columns(lastCol + 1).Copy Destination:=rws.Columns(10)
dws.Columns(lastCol + 2).Copy Destination:=rws.Columns(11)
'Create pivots.
Set pws = Sheets.Add
pws.Name = "Pivot"
Set start = rws.Range("A1")
Set dataRange = rws.Range(start, start.SpecialCells(xlLastCell))
startPvt = pws.Name & "!" & pws.Range("T1").Address(ReferenceStyle:=x1R1C1)
Set pvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=dataRange)
Set pvt = pvtCache.CreatePivotTable(TableDestination:=startPvt, TableName:="Market Totals")
pvt.PivotFields("Chart_Date_Group").Orientation = xlColumnField
pvt.PivotFields("Chart_Bin").Orientation = xlRowField
pvt.PivotFields("JOB NUMBER").Orientation = xlDataField
'Add slicers.
Dim sl As Slicer
Dim sls As Slicers
Dim slcs As SlicerCaches
Dim slc As SlicerCache
Set slcs = ActiveWorkbook.SlicerCaches
Set sls = slcs.Add(pws.PivotTables(1), "Carrier Type", "Carrier_Type").Slicers
Set sl = sls.Add(pws, , "Carrier_Type", "Carrier Type", 0, 0, 200, 75)
Set sls = slcs.Add(pws.PivotTables(1), "AVP", "AVP").Slicers
Set sl = sls.Add(pws, , "AVP", "AVP Type", 75, 0, 100, 250)
Set sls = slcs.Add(pws.PivotTables(1), "MARKET_RPA", "MARKET_RPA").Slicers
Set sl = sls.Add(pws, , "MARKET_RPA", "MARKET_RPA", 75, 100, 100, 400)
Set sls = slcs.Add(pws.PivotTables(1), "Driver", "Driver").Slicers
Set sl = sls.Add(pws, , "Driver", "Driver", 325, 0, 100, 150)
Set sls = slcs.Add(pws.PivotTables(1), "VENDOR", "VENDOR").Slicers
Set sl = sls.Add(pws, , "VENDOR", "VENDOR", 475, 0, 100, 150)
Set sls = slcs.Add(pws.PivotTables(1), "Hardware Location", "Hardware_Location").Slicers
Set sl = sls.Add(pws, , "Hardware_Location", "Hardware Location", 475, 100, 100, 200)
Set sls = slcs.Add(pws.PivotTables(1), "IWOS Flag", "IWOS_Flag").Slicers
Set sl = sls.Add(pws, , "IWOS_Flag", "IWOS Flag", 675, 0, 200, 125)
'Add data to data prep worksheet.
Dim dpws As Worksheet
Set dpws = Sheets.Add
dpws.Name = "Data Prep"
dpws.Cells(2, 1).Value = "Complete"
dpws.Cells(3, 1).Value = "Forecasted"
dpws.Cells(4, 1).Value = "Missed"
dpws.Cells(5, 1).Value = "Cumulative"
dpws.Cells(6, 1).Value = "Forecasted % Complete"
dpws.Cells(1, 2).Value = "2015"
dpws.Cells(1, 3).Value = "2016 Jan"
dpws.Cells(1, 4).Value = "2016 Feb"
dpws.Cells(1, 5).Value = "2016 Mar"
dpws.Cells(1, 6).Value = "2016 Apr"
dpws.Cells(1, 7).Value = "2016 May"
dpws.Cells(1, 8).Value = "2016 Jun"
dpws.Cells(1, 9).Value = "2016 Jul"
dpws.Cells(1, 10).Value = "2016 Aug"
dpws.Cells(1, 11).Value = "2016 Sep"
dpws.Cells(1, 12).Value = "2016 Oct"
dpws.Cells(1, 13).Value = "2016 Nov"
dpws.Cells(1, 14).Value = "2016 Dec"
dpws.Cells(1, 15).Value = "2017"
dpws.Cells(1, 16).Value = "2018"
For i = 2 To dpws.UsedRange.Columns.Count Step 1
dpws.Cells(2, i).Value = WorksheetFunction.IfError(pvt.GetPivotData("JOB NUMBER", "Chart_Bin", "Complete", "Chart_Date_Group", dpws.Cells(1, i).Value), 0)
dpws.Cells(3, i).Value = WorksheetFunction.IfError(pvt.GetPivotData("JOB NUMBER", "Chart_Bin", "Forecasted", "Chart_Date_Group", dpws.Cells(1, i).Value), 0)
dpws.Cells(4, i).Value = WorksheetFunction.IfError(pvt.GetPivotData("JOB NUMBER", "Chart_Bin", "Missed", "Chart_Date_Group", dpws.Cells(1, i).Value), 0)
Next i
dpws.Cells(1, 17).Value = "Grand Total"
dpws.Cells(2, i) = pvt.GetPivotData("JOB NUMBER", "Chart_Bin", "Complete")
dpws.Cells(3, i) = pvt.GetPivotData("JOB NUMBER", "Chart_Bin", "Forecasted")
dpws.Cells(4, i) = pvt.GetPivotData("JOB NUMBER", "Chart_Bin", "Missed")
dpws.Cells(5, i) = pvt.GetPivotData("JOB NUMBER")
'Calculate percentages/cumulatives.
Dim grandTotalCol As Integer
Dim percentageRow As Integer
Dim sumRow As Integer
Dim prevValue As Double
prevValue = 0
grandTotalCol = i
sumRow = 5
percentageRow = 6
With dpws
For i = 2 To dpws.UsedRange.Columns.Count Step 1
.Cells(sumRow, i).Value = WorksheetFunction.Sum(.Range(.Cells(2, i), .Cells(4, i))) + prevValue
prevValue = .Cells(sumRow, i).Value
If i = dpws.UsedRange.Columns.Count - 1 Then
prevValue = 0
End If
.Cells(percentageRow, i).Value = dpws.Cells(sumRow, i).Value / dpws.Cells(5, grandTotalCol).Value
.Cells(percentageRow, i).NumberFormat = "0%"
Next i
End With
'Plotting!
Dim dblMax As Double
dblMax = Application.WorksheetFunction.Max(dpws.Range("B2:P4"))
Dim chrt As Chart
Set chrt = pws.Shapes.AddChart.Chart
With chrt
.ChartArea.Left = 200
.ChartArea.Top = 0
.ChartArea.Height = 500
.ChartArea.Width = 800
.Legend.Position = xlLegendPositionBottom
.ChartType = xlColumnStacked
.HasDataTable = True
.SetSourceData Source:=dpws.UsedRange
.SeriesCollection("Forecasted % Complete").AxisGroup = 2
.SeriesCollection("Forecasted % Complete").ChartType = xlLineMarkers
.SeriesCollection("Forecasted % Complete").MarkerStyle = xlMarkerStyleSquare
.SeriesCollection("Cumulative").ChartType = xlLine
.SeriesCollection("Cumulative").Format.Line.Visible = False
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = dblMax + dblMax * 0.2
.Axes(xlValue, xlSecondary).MinimumScale = 0
.Axes(xlValue, xlSecondary).MaximumScale = 1
End With
End Sub
Just added 2 lines of code to your original 'Plotting Section
Dim dblMax As Double
dblMax = Application.WorksheetFunction.Max(dpws.Range("B2:P4"))
Dim chrt As Chart
Set chrt = pws.Shapes.AddChart.Chart
With chrt
.ChartArea.Left = 200
.ChartArea.Top = 0
.ChartArea.Height = 500
.ChartArea.Width = 800
.Legend.Position = xlLegendPositionBottom
.ChartType = xlColumnStacked
.HasDataTable = True
.SetSourceData Source:=dpws.UsedRange
.SeriesCollection("Forecasted % Complete").AxisGroup = 2
.SeriesCollection("Forecasted % Complete").ChartType = xlLineMarkers
.SeriesCollection("Forecasted % Complete").MarkerStyle = xlMarkerStyleSquare
.SeriesCollection("Cumulative").ChartType = xlLine
' Added the 2 lines below
.SeriesCollection("Cumulative").Format.Fill.Visible = msoFalse
.SeriesCollection("Cumulative").Format.Line.Visible = msoFalse
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = dblMax + dblMax * 0.2
.Axes(xlValue, xlSecondary).MinimumScale = 0
.Axes(xlValue, xlSecondary).MaximumScale = 1
nd With
I have been given the task of searching through a large volume of
data. The data is presented identically across around 50 worksheets. I
need a macro which searches through all these sheets for specific
values then copies certain cells to a table created in a new workbook.
The macro also needs to create the table headings when it is run.
It must Search column G For the Value 9.1 Then certain information
must be copied to corresponding columns in the table
FHA Ref = Same row value from column G
Engine Effect = Same row value from column F
Part Number = Always cell J3
Part Name = Always cell C2
FM ID = Same Row value from Column B
Failure Mode & Cause = Same Row Value from Column C
FMCN = Same Row Value From Column C"`
If it is a hassle to create the new workbook with these column
headings then I would be quite happy to create the headings myself in
the worksheet and just have the macro search for and copy the data to
the rows corresponding to the headings.
If any help or backup files are needed I would be more than happy to
provide these.
the code I have at the moment is based on a userform also ideally I would do away with this and just search all sheets
Public Sub createWSheet(module, srcWBook)
Dim i
i = 0
srcWB = srcWBook
For Each ws In Workbooks(srcWBook).Worksheets
i = i + 1
If ws.Name = module Then
MsgBox ("A worksheet with for this module already exists")
Exit Sub
End If
Next ws
Workbooks(srcWBook).Activate
Worksheets.Add after:=Worksheets(i)
ActiveSheet.Name = module
Cells(2, 2) = "FHA Ref"
Cells(2, 3) = "Engine Effect"
Cells(2, 4) = "Part No"
Cells(2, 5) = "Part Name"
Cells(2, 6) = "FM ID"
Cells(2, 7) = "Failure Mode & Cause"
Cells(2, 8) = "FMCN"
Cells(2, 9) = "PTR"
Cells(2, 10) = "ETR"
Range(Cells(2, 2), Cells(2, 10)).Font.Bold = True
Range(Cells(1, 2), Cells(1, 10)) = "Interface"
Range(Cells(1, 2), Cells(1, 10)).MergeCells = True
Range(Cells(1, 2), Cells(1, 10)).Font.Bold = True
Workbooks(srcWBook).Activate
End Sub
Dim mainWB, srcWBook
Dim headerLeft, headerTop, headerBottom, headerRight
Dim nTargetFMECA, nPartID, nLineID, nPartNo, nPartName, nQTY, nFailureMode, nAssumedSystemEffect, nAssumedEngineEffect
Dim item As String
Dim mDest
Dim selections(100)
Public Sub controlCopyFMs(mWB, sWB, module)
Dim i
mainWB = mWB
srcWBook = sWB
mDest = 2
nTargetFMECA = 0
nPartID = 0
nLineID = 0
nPartNo = 0
nPartName = 0
nQTY = 0
nFailureMode = 0
nAssumedSystemEffect = 0
nAssumedEngineEffect = 0
For i = 0 To TestForm.LBSelected.ListCount - 1
Call copyFMs(module, selections(i))
Next i
End Sub
Public Sub copyFMs(module, comp)
Dim mSrc
Workbooks(srcWBook).Sheets(comp).Select
If exploreHeader(comp) = 0 Then
Exit Sub
End If
mSrc = headerBottom + 3
While Cells(mSrc, nSrc).Text <> ""
If Cells(mSrc, nIndication).Text <> "-" Then
If Cells(mSrc, nIndication).Text <> "" Then
Workbooks(mainWB).Worksheets(module).Cells(mDest, 2) = Cells(mSrc, nTargetFMECA).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 3) = Cells(mSrc, nPartID).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 4) = Cells(mSrc, nLineID).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 5) = Cells(mSrc, nPartNo).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 6) = Cells(mSrc, nPartName).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 7) = Cells(mSrc, nQTY).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 8) = Cells(mSrc, nFailureMode).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 9) = Cells(mSrc, nAssumedEngineEffect).Value
Workbooks(mainWB).Worksheets(module).Cells(mDest, 10) = Cells(mSrc, nAssumedSystemEffect).Value
mDest = mDest + 1
End If
End If
mSrc = mSrc + 2
Wend
End Sub
Public Function exploreHeader(comp)
Dim m, n
m = 1
n = 1
While ((InStr(1, Cells(m, n).Text, "Engine Programme:", vbTextCompare) <= 0) Or (InStr(1, Cells(m, n).Text, "BR700-725", vbTextCompare) <= 0)) And n < 10
If m < 10 Then
m = m + 1
Else
n = n + 1
m = 1
End If
Wend
headerTop = m
headerLeft = n
While StrComp(Cells(m, n).Text, "ID", vbTextCompare) <> 0 And StrComp(Cells(m, n).Text, "Case No.", vbTextCompare) <> 0
m = m + 1
Wend
headerBottom = m - 1
While Cells(m, n).Borders(xlEdgeBottom).LineStyle = xlContinuous
n = n + 1
Wend
headerRight = n - 1
m = headerTop
n = headerLeft
Do
If n > headerRight Then
n = headerLeft
m = m + 1
End If
If InStr(1, Cells(m, n).Value, "Item No.:", vbTextCompare) > 0 Then
item = Right(Cells(m, n).Value, Len(Cells(m, n).Value) - InStr(1, Cells(m, n).Value, ":", vbTextCompare))
Cells(m, n).Select
Exit Do
End If
n = n + 1
Loop While m <= headerBottom
m = headerBottom + 1
n = headerLeft
While n <= headerRight
If StrComp(Cells(m, n).Value, "ID", vbTextCompare) = 0 Then
nID = n
End If
If StrComp(Cells(m, n).Value, "Mitigation", vbTextCompare) = 0 Then
nMitigation = n
End If
If StrComp(Cells(m, n).Value, "Remarks", vbTextCompare) = 0 Then
nRemarks = n
End If
If StrComp(Cells(m, n).Value, "FMCN", vbTextCompare) = 0 Then
nFMCN = n
End If
If StrComp(Cells(m, n).Value, "Indication", vbTextCompare) = 0 Then
nIndication = n
End If
If StrComp(Cells(m, n).Value, "Crit", vbTextCompare) = 0 Then
nFMCN = n
End If
If StrComp(Cells(m, n).Value, "Detect", vbTextCompare) = 0 Then
nIndication = n
End If
If StrComp(Cells(m, n).Value, "Functional Description", vbTextCompare) = 0 Then
nMitigation = n
End If
n = n + 1
Wend
exploreHeader = 1
End Function
Public Sub initSelections()
For i = 0 To 99
selections(i) = ""
Next i
End Sub
Public Sub loadSelection(comp, i)
selections(i) = comp
End Sub
Public Sub deleteSelection(i)
While selections(i) <> ""
selections(i) = selections(i + 1)
i = i + 1
Wend
End Sub
I hope this can help more. This code may not work 100% but it should be good enough to guide you. Let me know if you have questions.
Dim WS As Worksheet
Dim Results(7, 1000000) As String ''Didn't know what is a good data type or how many possible results
Dim ColValue() As Variant
Dim I, II, ResultCt As Long
ResultCt = 0
For Each WS In ActiveWorkbook.Worksheets ''This should get all your result and information into the Results Array
ColValue = ActiveSheet.Range(Cells(2, 7), Cells(WS.UsedRange.Rows.Count, 7)).Value ''This put all of column G into an array
For I = 0 To UBound(ColValue)
If ColValue(I, 1) = "9.1" Then
Results(0, ResultCt) = Cells(I + 1, 7).Value ''I think it is off by 1, but if not remove the +1
Results(1, ResultCt) = Cells(I + 1, 6).Value
Results(2, ResultCt) = Cells(3, 10).Value
Results(3, ResultCt) = Cells(2, 3).Value
Results(4, ResultCt) = Cells(I + 1, 2).Value
Results(5, ResultCt) = Cells(I + 1, 3).Value
Results(6, ResultCt) = Cells(I + 1, 3).Value
ResultCt = ResultCt + 1
End If
Next
Next WS
''At this point us your code to create the worksheet and name it
''starting from the line Workbooks(srcWBook).Activate
''Then Set the Active cell to where ever you want to start putting the data and have something like
For I = 0 To UBound(Results, 2)
For II = 0 To UBound(Results)
ActiveCell.Offset(I, II).Value = Results(I, II) ''This assumes you put the information into Result in the order you want it printed out
Next
Next
i want to make a program that saves the text in textbox to excel file using loop because i want to insert multiple text to excel. i found codes but it only overwrites data in cells. i want the program to find the last row and insert new data to the next row. im stuck here, please someone help me how to do that in vb.net. here is my code:
Excel = CreateObject("Excel.Application")
Excel.screenupdating = True
Excel.Visible = True
'fieldnames
Dim xlWorkSheet As Object = Excel.workbooks.add
Excel.workbooks(1).worksheets(1).cells(1, 1).value = "TITLE"
Excel.workbooks(1).worksheets(1).cells(1, 2).value = "AUTHOR"
Excel.workbooks(1).worksheets(1).cells(1, 3).value = "EDITION"
Excel.workbooks(1).worksheets(1).cells(1, 4).value = "PUBLISHER"
Excel.workbooks(1).worksheets(1).cells(1, 5).value = "ISBN"
'i want to loop here the data in textboxes
Excel.workbooks(1).worksheets(1).cells(2, 1).value = txtTitle.Text
Excel.workbooks(1).worksheets(1).cells(2, 2).value = txtAuthor.Text
Excel.workbooks(1).worksheets(1).cells(2, 3).value = txtEdition.Text
Excel.workbooks(1).worksheets(1).cells(2, 4).value = txtPublisher.Text
Excel.workbooks(1).worksheets(1).cells(2, 5).value = txtISBN.Text
xlWorkSheet.SaveAs(FileName)
Excel.quit()
Excel = Nothing
you're going to need to dynamically set the value for the rowindex and the aplha column.
so something like this
dim currRow as integer = 0
Excel = CreateObject("Excel.Application")
Excel.screenupdating = True
Excel.Visible = True
'fieldnames
Dim xlWorkSheet As Object = Excel.workbooks.add
Excel.workbooks(1).worksheets(1).cells((currRow+1), 1).value = "TITLE"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 2).value = "AUTHOR"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 3).value = "EDITION"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 4).value = "PUBLISHER"
Excel.workbooks(1).worksheets(1).cells((currRow+1), 5).value = "ISBN"
currRow += 1
for i as integer = currRow to 5
'i want to loop here the data in textboxes
Excel.workbooks(1).worksheets(1).cells((currRow + i), 1).value = txtTitle.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 2).value = txtAuthor.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 3).value = txtEdition.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 4).value = txtPublisher.Text
Excel.workbooks(1).worksheets(1).cells((currRow + i), 5).value = txtISBN.Text
next
currRow += 1
xlWorkSheet.SaveAs(FileName)
Excel.quit()
Excel = Nothing
i don't know if this works but this is something to get you started.