在VBA中使用MID和LEFT连接

时间:2015-12-01 00:25:41

标签: excel vba excel-vba

我正在编写一个宏,它允许我打开一个文件,插入3行并连接文件的各个部分,以允许我创建一个我可以在VLOOKUP中引用的唯一字符串。我正在尝试使用公式 CONCATENATE(中(E8,7,2),“/”,中(E8,5,2),“/”,左(E8,4))我的代码看起来像:

Sub OpenDCSheet()

Dim OpenFileName As String
Dim wb As Workbook
Dim LastRow As Long

MasterSheet = ActiveWorkbook.Name

'Select and Open workbook
MsgBox ("Please select the data file")
OpenFileName = Application.GetOpenFilename
If OpenFileName = "False" Then Exit Sub
Set wb = Workbooks.Open(OpenFileName, UpdateLinks = 0)

DoubleClickSheet = ActiveWorkbook.Name

  Windows(DoubleClickSheet).Activate
'Inserting a Column at Column B
[B3].Resize(, 3).EntireColumn.Insert

Range("B8").Select
    ActiveCell.Formula = CONCATENATE(Mid(E8, 7, 2), "/", Mid(E8, 5, 2), "/", Left(E8, 4))

有人可以解决为什么这不起作用吗?

谢谢,

亚伦

2 个答案:

答案 0 :(得分:1)

你非常接近。考虑:

 private int pageID = VIEW_MENU;
        private Stack<Integer> pageSeq = new Stack<Integer>();
    .
    .
    .
    public void menuAction(int id)
        {
            if(id == R.id.actionbar_search)
            {
                ((SearchListener)fragment).action();
                if(isSliderMenuOpen)
                    mDrawerLayout.closeDrawer(mDrawerList);
                return;
            }
            int page = -1;
            switch (id) {
                case android.R.id.home :

                if(this.pageSeq.size() > 0)
                {
                    if(pageID == VIEW_BOTANICAL_DETAIL)
                    {
                        showActionBarMenu();
                    }
                    page = this.pageSeq.pop();
                }
                else if(isSliderMenuOpen)
                    mDrawerLayout.closeDrawer(mDrawerList);
                else
                    mDrawerLayout.openDrawer(mDrawerList);

                break;
            case SIDE_MENU_MAP:
            case R.id.actionbar_map: 
                page = VIEW_MAP;
                ActionBar map = getActionBar();
                map.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FECC66")));
                if(this.pageID == VIEW_HOME || this.pageID == VIEW_BOTANICAL_DETAIL || this.pageID == VIEW_ALL
                        || this.pageID == VIEW_HOT|| this.pageID == VIEW_RAIN|| this.pageID == VIEW_COOL|| this.pageID == VIEW_REC)
                    pushPageSeq();
                break;
            case SIDE_MENU_AR:
            case R.id.actionbar_ar: 
                ActionBar ar = getActionBar();
                ar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FECC66")));
                if(!((LocationManager) getSystemService( Context.LOCATION_SERVICE )).isProviderEnabled( LocationManager.GPS_PROVIDER )){
                    warningLocationServiceNotFound();
                    return;
                }
                page = VIEW_AR;
                if(this.pageID == VIEW_HOME|| this.pageID == VIEW_ALL
                        || this.pageID == VIEW_HOT|| this.pageID == VIEW_RAIN|| this.pageID == VIEW_COOL|| this.pageID == VIEW_REC)
                    pushPageSeq();
                break;

            case SIDE_MENU_ABOUT:
                page = VIEW_ABOUT;
                if(this.pageID == VIEW_HOME|| this.pageID == VIEW_ALL
                        || this.pageID == VIEW_HOT|| this.pageID == VIEW_RAIN|| this.pageID == VIEW_COOL|| this.pageID == VIEW_REC)
                    pushPageSeq();
                break;

            case SIDE_MENU_ALL:
                page = VIEW_ALL;
                showActionBarMenu(R.id.actionbar_search);
                ActionBar all = getActionBar();
                all.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#709256")));
                Toast.makeText(this, "All..", Toast.LENGTH_SHORT)
                .show();
                if(this.pageID == VIEW_HOME || this.pageID == VIEW_MENU)
                    //pushPageSeq();
                break;
            case SIDE_MENU_HOT:
                page = VIEW_HOT;
                showActionBarMenu(R.id.actionbar_search);
                ActionBar hot = getActionBar();
                hot.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ef5c26")));
                Toast.makeText(this, "Hot..", Toast.LENGTH_SHORT)
                .show();
                if(this.pageID == VIEW_HOME || this.pageID == VIEW_MENU)
                    //pushPageSeq();
                break;
            case SIDE_MENU_RAIN:
                page = VIEW_RAIN;
                showActionBarMenu(R.id.actionbar_search);
                ActionBar rain = getActionBar();
                rain.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#a89c9c")));
                Toast.makeText(this, "Rain..", Toast.LENGTH_SHORT)
                .show();
                if(this.pageID == VIEW_HOME || this.pageID == VIEW_MENU)
                    //pushPageSeq();
                break;
            case SIDE_MENU_COOL:
                page = VIEW_COOL;
                showActionBarMenu(R.id.actionbar_search);
                ActionBar cool = getActionBar();
                cool.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4e70b0")));
                Toast.makeText(this, "Cool..", Toast.LENGTH_SHORT)
                .show();
                if(this.pageID == VIEW_HOME || this.pageID == VIEW_MENU)
                    //pushPageSeq();
                break;
            case SIDE_MENU_REC:
                page = VIEW_REC;
                showActionBarMenu(R.id.actionbar_search);
                ActionBar rec = getActionBar();
                rec.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f3424a")));
                Toast.makeText(this, "Rec..", Toast.LENGTH_SHORT)
                .show();
                if(this.pageID == VIEW_HOME || this.pageID == VIEW_MENU)
                    //pushPageSeq();
                break;
            default:
                page = VIEW_MENU; 
                break;
        }
        if(page >= 0 ){
            if(page != pageID)
            {
                if(this.pageID == VIEW_ABOUT  ||this.pageID == VIEW_AR || this.pageID==VIEW_MENU )
                    showActionBarMenu(R.id.actionbar_search);
                displayView(page);
            }   
        }
    }

这只解决了这一行代码中的错误。您可能还有其他问题。

答案 1 :(得分:1)

如果您计划Vlookup唯一的字符串。

我建议您使用用户定义函数(UDF)加入。 而是使用连接。

以下是两者之间的差异: enter image description here

处理日期,连接将带来另一个问题。

以下是我创建的UDF:

Function joinCell(ByVal inputRange As Range) As String
Dim nCell As Range
Dim nText As String
    For Each nCell In inputRange.Cells
        nText = nText & Trim(CStr(nCell.Value))
    Next
    joinCell = nText
End Function

希望得到这个帮助。