I have .xlsx sheet where data starts from A1:AB199. I am trying to extract data from Row A6:AB48 and ignore the rest.
Started creating a Conditional Split so SSIS package can start from Row A6 and end at Row AB48 but failing. Please guide
Try this, In your Data Flow task, you will need to set the "OpenRowset" Custom Property of your Excel Connection
Or
Another MSDN Link
1 - Excel Source -> Variables -> In Data Access Mode select "Table name or view name variable"
2- In the variable name select a variable that you had made before "MyVar"
3- Go To variable select "MyVar" and type "TabName$A12:H125"
Before conditional split add a script component with one output column of type DT_BOOL . In my example i assume that is named OutColumn.
In the script window add the following Code:
Private m_intRowCounter as integer = 0
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
m_intRowCounter += 1
if m_intRowCounter >= 6
Row.OutCOlumn = True
Else
Row.OutCOlumn = False
End If
End Sub
In the conditional split split row on OutColumn : if true take rows to destination
Hope it helps
Related
I am running a script from Excel and would like to loop through rows in a table in order to obtain values for each line in a table. Here is what I basically need to do:
1.Go to a page with a table in SAP
Double click on a line, which brings me to a window with values that I need to copy and insert in a spreadsheet.
Loop through an entire table in the same fashion and repeat a procedure in SAP.
The attached pictures show tables I am working with. I am not sure which of them are real tables that can be declared as Objects. I tried to run a code but it returns an error "The object does not support this property or method"(RowCount)
first type of object
second type of object
third type of object
Dim Table As Object
Dim rows As Long
Dim i As Long
Set Table = Session.FindById("wnd[0]/usr/cntlUSAGE_TREE_CONTAINER/shellcont/shell/shellcont[1]/shell[1]")
rows = Table.RowCount - 1
For i = 0 To rows
Session.FindById("wnd[0]/usr/cntlUSAGE_TREE_CONTAINER/shellcont/shell/shellcont[1]/shell").selectedNode = " i"
Session.FindById("wnd[0]/usr/cntlUSAGE_TREE_CONTAINER/shellcont/shell/shellcont[1]/shell").doubleClickNode " i"
Next i
In my opinion, the attached pictures contain the following contents:
Tree
Table
Grid
The program example refers to a tree.One of the possible solutions might look like this:
Set myTree = Session.FindById("wnd[0]/usr/cntlUSAGE_TREE_CONTAINER/shellcont/shell/shellcont[1]/shell[1]")
rowCount = myTree.GetColumnCol(myTree.GetColumnNames.item(0)).length
rows = rowCount - 1
For i = 0 To rows
myTree.selectedNode right(" " + CStr(i),11)
myTree.doubleClickNode right(" " + CStr(i),11)
Next i
Regards,
ScriptMan
In an Excel VBA module I'm building, I have some code to execute if a table of out-of-gauge cargo contains anything. I initially wrote this:
If Not IsEmpty(Range("OOGData")) Then
...
Else
...
End If
But even when OOGData is empty, it keeps returning False. I've tried it with If IsEmpty(Range("OOGData")) = False Then` but that doesn't seem to make any difference. My current code is
If IsEmpty(Range("OOGData")) = False Then
...but that still activates with the empty range.
I've made sure there are no formulae, hidden values or anything that could be showing up.
Any idea what the problem could be?
According to this information:
Returns a Boolean value indicating whether a variable has been
initialized.
In your code you are not working with variable therefore you shouldn't expect correct value.
When checking single cell you should use Len() function instead:
If Len(Range("OOGData"))=0 Then
'cell is empty
When checking if range of cells is empty use this solution:
If WorksheetFunction.CountA(Range("OOGData"))=0 Then
'rabge is empty
The final alternative I can think of is to use loops.
I've decided to cheat. Instead of checking the list there, I've added a Boolean variable (bContainsOOG) that's set to True any time an OOG cargo item is added to the OOG list, and then the reporting sub checks against that instead. But thanks both of you for those suggestions, they'll come in handy in another bit I was getting stuck on. :-)
This is a function that I thinks very closely fits:-
' Function to determine the last (first blank/null/empty) cell in a series of data in either a column
' or row
' Usage :- DataSeriesEnd(w,r,b)
' where w is the name of the worksheet.
' r is the row or column as an integer
' b is a boolean (true/false) if true then processing a column, if false a row
Public Function DataSeriesEnd(Worksheet As String, rc_index As Integer, bycolumn As Boolean) As Integer
Dim cv As Variant
For DataSeriesEnd = 1 To 500
If bycolumn Then
cv = Worksheets(Worksheet).Cells(DataSeriesEnd, rc_index)
Else
cv = Worksheets(Worksheet).Cells(rc_index, DataSeriesEnd)
End If
If IsNull(cv) Or IsEmpty(cv) Or cv = "" Then Exit For
Next DataSeriesEnd
' Position to previous cell (i.e. this one is empty)
DataSeriesEnd = DataSeriesEnd - 1
End Function
Note! There is a limit of 500 rows/columns
I am a beginner in VBA programming.
Description: The left table is the 'reference table'
Objective: Fill up the 'Type' col of right table using macro
How: Write a macro that go through the 'reference table' comparing col E (Descript) with keyword. If the cell in col E contain a specific key word, col F will automatically be assigned a category
P.S: What are the recommended websites that provide tutorial? Something like codecademy
So still stuck at correctly referencing table:
thanks for editing your answer to include the code. Next time please copy/paste it, rather than using an image, so that we don't have to type it all out ourselves if we need to test what you've written! I can see where you're at now, and I feel this will work you for. Basically, you need to cycle through the rows rather than the columns.
Rather than using a subroutine, I think a function will work better for you. That way you can embed it in your table, rather than having to run a subroutine each time.
Function getCategory(strInput As String)
Dim tbl As ListObject
Dim x As Long
Set tbl = ActiveSheet.ListObjects("Table1")
For x = 1 To tbl.ListRows.Count
If InStr(strInput, tbl.ListRows(x).Range(x, 1)) Then
getCategory = tbl.ListRows(x).Range(x, 2)
Exit Function
End If
Next x
End Function
Then you can simply enter the formula in column F, e.g. in F2 enter
=getCategory(E2)
and copy/paste down for each row in your table.
I am using VBA to iterate through rows in a table.
For each row the code (where I refer to the rows as "scenario")
opens a file path specified in column 6,
does something, and
closes the file path specified in column 6
I am trying to execute the code below; however, it is failing because the file name must be specified as a string. I cannot do this because the file name will be different for each row in my table
Workbooks("scenario.Columns(6)", False).Close
What is the proper syntax to close a file using a cell reference for the name of the file?
Much appreciated!
1. If scenario range contains one row, to get the value in sixth column you need to use:
Workbooks(scenario.Cells(,6)).Close False
2. If scenario range contains few rows, to get the value in sixth column you need to specify row number as well (i.e. second):
Workbooks(scenario.Cells(2,6)).Close False
UPD:
Try this one (following from comments):
Dim Scenario As Range
Dim input_table As Range
'Identifying input table
Set input_table = Sheets("Input").[Scenario_input]
'iterate through rows in table
For Each Scenario In input_table.Rows
If Scenario.Columns(5).Value = "Yes" Then
Workbooks.Open Filename:=Scenario.Columns(6).Value
'do something
'
'Close the scenario workbook
Workbooks(Scenario.Columns(6).Value).Close False
End If
Next
The data I have can be simplified to this:
http://i.imgur.com/mn5GgrQ.png
In this example, I would like to delete the data associated with track 2, since it has only 3 frames associated with it. All data with more than 3 associated frames can stay.
The frame number does not always start from 1, as I've tried to demonstrate. The track number will always be the same number consecutively for as many frames as are tracked. I was thinking of using a function to append 1 to a variable for every consecutive value in column A, then performing a test to see if this value is equal >= 3. If so, then go onto the next integer in A, if no, then delete all rows marked with that integer (2, in this case).
Is this possible with Visual Basic in an Excel Macro, and can anyone give me some starting tips on what functions I might be able to use? Complete novice here. I haven't found anything similar for VBA, only for R.
I assume you understand the code by reading it.
Option Explicit
Public Function GetCountOfRowsForEachTrack(ByVal sourceColumn As Range) As _
Scripting.Dictionary
Dim cell As Range
Dim trackValue As String
Dim groupedData As Scripting.Dictionary
Set groupedData = New Scripting.Dictionary
For Each cell In sourceColumn
trackValue = cell.Value
If groupedData.Exists(trackValue) Then
groupedData(trackValue) = cell.Address(False, False) + "," + groupedData(trackValue)
Else
groupedData(trackValue) = cell.Address(False, False)
End If
Next
Set GetCountOfRowsForEachTrack = groupedData
End Function
Public Sub DeleteRowsWhereTrackLTE3()
Dim groupedData As Scripting.Dictionary
Set groupedData = GetCountOfRowsForEachTrack(Range("A2:A15"))
Dim cellsToBeDeleted As String
Dim item
For Each item In groupedData.Items
If UBound(Split(item, ",")) <= 2 Then
cellsToBeDeleted = item + IIf(cellsToBeDeleted <> "", "," + cellsToBeDeleted, "")
End If
Next
Range(cellsToBeDeleted).EntireRow.Delete
End Sub
GetCountOfRowsForEachTrack is a function returning a dictionary (which stores track number as key, cell address associated with that track as string)
DeleteRowsWhereTrackLTE3 is the procedure which uses GetCountOfRowsForEachTrack to get the aggregated info of Track numbers and cells associated with it. This method loops through the dictionary and checks if the number of cells associated with track is <=2 (because splitting the string returns an array which starts from 0). It builds a string of address of such cells and deletes it all at once towards the end.
Note:
Add the following code in a bas module (or a specific sheet where
you have the data).
Add reference to "Microsoft Scripting.Runtime" library. Inside VBA, click on "Tools" -> "References" menu. Tick the "Microsoft Scripting.Runtime" and click on OK.
I have used A2:A15 as an example. Please modify it as per your cell range.
The assumption is that you don't have thousands of cells to be deleted, in which case the method could fail.
Make a call to DeleteRowsWhereTrackLTE3 to remove such rows.