cntlMEALV_GRID_CONTROL_MMHIPO/shellcont/shell").selectColumn "XBLNR"
I'm trying to extract this into excel.
I was only able to extract row count using watch but not sure how extract this using excel
Any code or sample or help will be appreciated thanks.
Use getcellValue to cast to excel.
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set session = SAPCon.Children(0)
MyGrid = "cntlMEALV_GRID_CONTROL_MMHIPO/shellcont/shell"
iRows = session.findById(MyGrid).RowCount()
'Fill excel sheet
Do While i < iRows
Cells(i + 1, 1).Value = session.findById(MyGrid).getcellValue(i, "XBLNR")
i = i + 1
Loop
Related
I have over 200 sheets in an Excel workbook that are each formatted in a really odd way and I need to figure out how to compile all the data that I need into a single master sheet. I only need the values from certain cells and ranges (shown in the code below). I'd like the final compiled sheet to be in long-form (see attached image).
There is an attached image that is an example of the format of each sheet - it contains all the cells but does not contain any actual data. In reality, there is a lot of data - some sheets have >1000 rows.
I tried to use a function in R to read in all the sheets as separate data frames so that I could merge them but I couldn't get it to work. I then tried to use VBA, but I'm not familiar with the syntax. Here's what I came up with:
Sub Copy_Example()
Dim J As Integer
Dim s As Worksheet
On Error Resume Next
Sheets(1).Select
Worksheets.Add ' add a sheet in first place
Sheets(1).Name = "Combined"
Worksheets("Sheet2").Range("D9").Copy Destination:=Worksheets("Combined").Range("A2")
Worksheets("Sheet2").Range("E2").Copy Destination:=Worksheets("Combined").Range("B2")
Worksheets("Sheet2").Range("E3").Copy Destination:=Worksheets("Combined").Range("C2")
Worksheets("Sheet2").Range("E4").Copy Destination:=Worksheets("Combined").Range("D2")
Worksheets("Sheet2").Range("E5").Copy Destination:=Worksheets("Combined").Range("E2")
Worksheets("Sheet2").Range("C22:C2000").Copy Destination:=Worksheets("Combined").Range("F1")
Worksheets("Sheet2").Range("E22:E2000").Copy Destination:=Worksheets("Combined").Range("G1")
Worksheets("Sheet2").Range("F22:F2000").Copy Destination:=Worksheets("Combined").Range("H1")
Worksheets("Sheet2").Range("G22:G2000").Copy Destination:=Worksheets("Combined").Range("I1")
Worksheets("Sheet2").Range("H22:H2000").Copy Destination:=Worksheets("Combined").Range("J1")
Worksheets("Sheet2").Range("I22:I2000").Copy Destination:=Worksheets("Combined").Range("K1")
End Sub
This VBA will copy and paste the correct columns and ranges into a newly created worksheet only for Sheet 2. I tried to integrate additional snippets of code so that this would run through all sheets in the workbook and paste the data below the last line previously added but I can't get it to work. I would also love to be able to add a column with the name of the sheet that the data has been copied from.
If anyone can help me with this, using either R or VBA, I would really appreciate it.
This is an example of the format of each sheet
This is an example of what I'd like the master compiled sheet to look like
Try the below code
Sub CopyToCombined()
Dim oComWS As Worksheet, oWS As Worksheet
Dim iLR As Long: iLR = 1
' Add New sheet as "Combined"
Set oComWS = ThisWorkbook.Worksheets.Add
oComWS.Name = "Combined"
' Loop through all sheets in the workbook and copy details in Combined sheet
For Each oWS In ThisWorkbook.Worksheets
If oWS.Name <> "Combined" Then
With oWS
oComWS.Range("A" & iLR).Value = .Range("A3").Value
oComWS.Range("B" & iLR).Value = .Range("B5").Value
oComWS.Range("C" & iLR).Value = .Range("C26").Value
End With
iLR = iLR + 1
End If
Next
End Sub
Above code will go through all sheets in your workbook and copy the relevant data (obviously you will have to change what you want to copy)
EDIT 1:
As per requirement, below code should update the Combined as you requested
Sub CopyToCombined()
Dim oComWS As Worksheet, oWS As Worksheet
Dim iLR As Long: iLR = 1
Dim iC As Long
Dim aCleanArray As Variant, aMyRange As Variant, aColumn As Variant
' Add New sheet as "Combined"
Set oComWS = ThisWorkbook.Worksheets.Add
oComWS.Name = "Combined"
' Set arrays
aMyRange = Array("C20:C50", "D20:D50") ' <-- Set all your ranges here (i.e. "C22:C2000", "E22:E2000", ...)
aColumn = Array("C", "D") ' <-- Set the columns here (i.e. "F", "G", ...)
' Loop through all sheets in the workbook and copy details in Combined sheet
For Each oWS In ThisWorkbook.Worksheets
If oWS.Name <> "Combined" Then
With oWS
oComWS.Range("A" & iLR).Value = .Range("A2").Value
oComWS.Range("B" & iLR).Value = .Range("B2").Value
For iC = LBound(aMyRange) To UBound(aMyRange)
aCleanArray = CleanUpArray(.Range(aMyRange(iC)).Value)
oComWS.Range(aColumn(iC) & iLR & ":" & aColumn(iC) & (iLR + UBound(aCleanArray))).Value = Application.Transpose(aCleanArray)
Next
End With
iLR = oComWS.Range(aColumn(0) & oComWS.Rows.Count).End(xlUp).Row + 1
End If
Next
End Sub
Function CleanUpArray(aIncomigArray As Variant) As Variant
Dim aTemp() As Variant
Dim iC As Long
ReDim aTemp(0 To 0)
For iC = LBound(aIncomigArray) To UBound(aIncomigArray)
If Not IsEmpty(aIncomigArray(iC, 1)) Then
aTemp(UBound(aTemp)) = aIncomigArray(iC, 1)
ReDim Preserve aTemp(UBound(aTemp) + 1)
End If
Next
ReDim Preserve aTemp(UBound(aTemp) - 1)
CleanUpArray = aTemp
End Function
Hope this helps
I modify a cell in an Excel file using R.
When this cell is manually changed, Worksheet_Change launches to indicate the date of the modification (in two other cells).
When I run my code in R, the date of the modification in column 46 (see VBA code ModificationDate1) is indicated. The date of the modification in column 40 (see VBA code ModificationDate2) doesn't appear. I get a VBA error 1004.
It is not indicated where the error appears. I can't click debug.
I would like the date of the modification in column 40 to also appear. (This later appears when I make a change directly in Excel.)
My code in R:
if (NewInput != CurrentData) {
xlApp <- COMCreate("Excel.Application")
wb <- xlApp[["Workbooks"]]$Open("path.xlsm")
sheet <- wb$Worksheets("Sheet1")
cell <- sheet$Cells(Outputrow + 6, 7)
cell[["Value"]] <- paste0(NewInput)
wb$Save()
xlApp$Quit()
}
My codes in vba:
Private Sub Worksheet_Change(ByVal Target As Range)
'**ModificationDate1**
Dim WorkRng As Range
Dim Rng As Range
LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = Range(Cells(7, 7), Cells(LastRow, 7))
If Not Intersect(Target, Rng) Is Nothing Then
Call UnprotectCells
Cells(Target.Row, 46).Value = Date
Call ProtectCells
End If
'**ModificationDate2**
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range(Cells(7, 3), Cells(LastRow, 4))
Set rng2 = Range(Cells(7, 7), Cells(LastRow, 10))
If Not Intersect(Target, Union(rng1, rng2)) Is Nothing Then
Call UnprotectCells
Cells(Target.Row, 40).Value = Date
Call ProtectCells
End If
End Sub
I have a model in excel and trying to feed inputs through ASP.NET to the excel and returning the back the model results over the grid in the front end. I'm using EPPlus package in ASP.NET as it doesn't need MS Office to be installed.
I have trouble getting the value of the cell where NPV [Net Present Value] and IRR [Internal rate of return] functions have been used. It always returns #VALUE or #NAME. As a workaround, I was able to calculate NPV manually but I'm having a tough time with IRR.
Is there a way in EPPlus to paste all the cells as values in the worksheet?
Or is there any other alternative?
Please help and thanks in advance
Update : I have EPPlus 4.5.3.1 and Visual studio 2013.
string strfilepath = Server.MapPath(destPath);
FileInfo fileInfo = new FileInfo(strfilepath);
ExcelPackage p = new ExcelPackage(fileInfo);
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Financial"];
GridView1.Rows[0].Cells[2].Text = float.Parse(myWorksheet1.Cells["f57"].Value.ToString()).ToString("N2");
I have tried all possible ways in EPPlus but I was not able to calculate IRR. I came up with a solution in SQL and it gives the result close to excel 's IRR function.
CREATE FUNCTION [dbo].[Ufn_irr1] (#strIDs1 VARCHAR(10),
#strIDs2 VARCHAR(10),
#guess DECIMAL(30, 10))
returns DECIMAL(30, 10)
AS
BEGIN
DECLARE #t_IDs TABLE
( a
id INT IDENTITY(0, 1),
value DECIMAL(30, 10)
)
DECLARE #strIDs VARCHAR(max)
SET #strIDs = #strIDs1 + ',' + #strIDs2
DECLARE #strID VARCHAR(12),
#sepPos INT,
#NPV DECIMAL(30, 10)
SET #strIDs = COALESCE(#strIDs + ',', '')
SET #sepPos = Charindex(',', #strIDs)
WHILE #sepPos > 0
BEGIN
SET #strID = LEFT(#strIDs, #sepPos - 1)
INSERT INTO #t_IDs
(value)
SELECT ( Cast(#strID AS DECIMAL(20, 10)) )
WHERE Isnumeric(#strID) = 1
SET #strIDs = RIGHT(#strIDs, Datalength(#strIDs) - #sepPos)
SET #sepPos = Charindex(',', #strIDs)
END
SET #guess = CASE
WHEN Isnull(#guess, 0) <= 0 THEN 0.00001
ELSE #guess
END
SELECT #NPV = Sum(value / Power(1 + #guess, id))
FROM #t_IDs
WHILE #NPV > 0
BEGIN
SET #guess = #guess + 0.00001
SELECT #NPV = Sum(value / Power(1 + #guess, id))
FROM #t_IDs
END
RETURN #guess
END
I need to read a 10GB fixed width file to a dataframe. How can I do it using Spark in R?
Suppose my text data is the following:
text <- c("0001BRAjonh ",
"0002USAmarina ",
"0003GBPcharles")
I want the 4 first characters to be associated to the column "ID" of a data frame; from character 5-7 would be associated to a column "Country"; and from character 8-14 to be associated to a column "Name"
I would use function read.fwf if the dataset was small, but that is not the case.
I can read the file as a text file using sparklyr::spark_read_text function. But I don't know how to attribute the values of the file to a data frame properly.
EDIT: Forgot to say substring starts at 1 and array starts at 0, because reasons.
Going through and adding the code I talked about in the column above.
The process is dynamic and is based off a Hive table called Input_Table. The table has 5 columns: Table_Name, Column_Name, Column_Ordinal_Position, Column_Start, and Column_Length. It is external so any user can change, drop, and remove any file into the folder location. I quickly built this from scratch to not take actual code, does everything make sense?
#Call Input DataFrame and the Hive Table. For hive table we make sure to only take correct column as well as the columns in correct order.
val inputDF = spark.read.format(recordFormat).option("header","false").load(folderLocation + "/" + tableName + "." + tableFormat).rdd.toDF("Odd_Long_Name")
val inputSchemaDF = spark.sql("select * from Input_Table where Table_Name = '" + tableName + "'").sort($"Column_Ordinal_Position")
#Build all the arrays from the columns, rdd to map to collect changes a dataframe col to a array of strings. In this format I can iterator through the column.
val columnNameArray = inputSchemaDF.selectExpr("Column_Name").rdd.map(x=>x.mkString).collect
val columnStartArray = inputSchemaDF.selectExpr("Column_Start_Position").rdd.map(x=>x.mkString).collect
val columnLengthArray = inputSchemaDF.selectExpr("Column_Length").rdd.map(x=>x.mkString).collect
#Make the iteraros as well as other variables that are meant to be overwritten
var columnAllocationIterator = 1
var localCommand = ""
var commandArray = Array("")
#Loop as there are as many columns in input table
while (columnAllocationIterator <= columnNameArray.length) {
#overwrite the string command with the new command, thought odd long name was too accurate to not place into the code
localCommand = "substring(Odd_Long_Name, " + columnStartArray(columnAllocationIterator-1) + ", " + columnLengthArray(columnAllocationIterator-1) + ") as " + columnNameArray(columnAllocationIterator-1)
#If the code is running the first time it overwrites the command array, else it just appends
if (columnAllocationIterator==1) {
commandArray = Array(localCommand)
} else {
commandArray = commandArray ++ Array(localCommand)
}
#I really like iterating my iterators like this
columnAllocationIterator = columnAllocationIterator + 1
}
#Run all elements of the string array indepently against the table
val finalDF = inputDF.selectExpr(commandArray:_*)
I'm writing in ASP.NET 4 / VB.NET. I am querying an MSSQL database and sometimes have records come back with no results...so I enclosed the call I was making upon the results in an If..Else clause to set a default value if the database comes back with no results...but now I am getting this "Object variable or With block variable not set error". Here is the relevant code:
Dim clcfirst
Dim rhcfirst
Dim clcdate As Date
Dim rhcdate As Date
If IsNothing(clcexists) Then
clcfirst = Date.Now.Subtract(year)
rhcfirst = Date.Now.Subtract(year)
clcdate = clcfirst
rhcdate = rhcfirst
Else
clcfirst = clcexists.FirstOrDefault()
rhcfirst = rhcexists.FirstOrDefault()
clcdate = clcfirst.SignatureDate
rhcdate = rhcfirst.SignatureDate
End If
Where is the DateTime year variable being set? Could that be null?
If you want to subtract a year, you could just do:
clcdate = Date.Now.AddYears(-1)
rhcdate = Date.Now.AddYears(-1)